You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>We can use the String in <code>content</code> after the match block.
553
-
If <code>result</code> were an error, the String wouldn’t exist.
554
-
But since the program would exit before it ever reached a point where we use <code>content</code>,
555
-
it’s fine.</p>
551
+
<p>We can use the String in <code>content</code> after the match block, but
552
+
if <code>result</code> were an error, the String wouldn’t exist.
553
+
That’s fine because the program would exit before it ever reached a point where we use <code>content</code>.</p>
556
554
<p>This may seem drastic,
557
555
but it’s very convenient.
558
556
If your program needs to read that file and can’t do anything if the file doesn’t exist,
559
557
exiting is a valid strategy.
560
-
There’s even a shortcut method on <code>Result</code>s, called <code>unwrap</code>:</p>
558
+
There’s even a shortcut method on <ahref="https://doc.rust-lang.org/1.39.0/std/result/index.html"><code>Result</code></a> called <code>unwrap</code>:</p>
that are not required to understand to work with this.
628
626
For example,
629
-
the error type in our <code>main</code> function is <code>Box<dyn std::error::Error></code>.
630
-
But we’ve seen above that <code>read_to_string</code> returns a <ahref="https://doc.rust-lang.org/1.39.0/std/io/type.Result.html"><code>std::io::Error</code></a>.
627
+
the error type in our <code>main</code> function is <code>Box<dyn std::error::Error></code>,
628
+
but we’ve seen above that <code>read_to_string</code> returns a <ahref="https://doc.rust-lang.org/1.39.0/std/io/type.Result.html"><code>std::io::Error</code></a>.
631
629
This works because <code>?</code> expands to code that <em>converts</em> error types.</p>
632
630
<p><code>Box<dyn std::error::Error></code> is also an interesting type.
633
631
It’s a <code>Box</code> that can contain <em>any</em> type
634
632
that implements the standard <ahref="https://doc.rust-lang.org/1.39.0/std/error/trait.Error.html"><code>Error</code></a> trait.
635
-
This means that basically all errors can be put into this box,
636
-
so we can use <code>?</code> on all of the usual functions that return <code>Result</code>s.</p>
633
+
This means that all errors can be put into this box,
634
+
and we can use <code>?</code> on all of the usual functions that return a <code>Result</code>.</p>
running this we’ll get our custom error message:</p>
661
+
<p>Running this, we’ll get our custom error message:</p>
665
662
<pre><codeclass="language-text">Error: CustomError("Error reading `test.txt`: No such file or directory (os error 2)")
666
663
</code></pre>
667
664
<p>Not very pretty,
668
-
but we can easily adapt the debug output for our type later on.</p>
669
-
<p>This pattern is in fact very common.
670
-
It has one problem, though:
665
+
but we can adapt the debug output for our type later on.</p>
666
+
<p>This pattern is very common.
667
+
It has one problem though:
671
668
We don’t store the original error,
672
669
only its string representation.
673
-
The often used <ahref="https://docs.rs/anyhow"><code>anyhow</code></a> library has a neat solution for that:
674
-
similar to our <code>CustomError</code> type,
675
-
its <ahref="https://docs.rs/anyhow/1.0/anyhow/trait.Context.html"><code>Context</code></a> trait can be used to add a description.
676
-
Additionally, it also keeps the original error,
677
-
so we get a “chain” of error messages pointing out the root cause.</p>
670
+
The popular <ahref="https://docs.rs/anyhow"><code>anyhow</code></a> library has a neat solution for that:
671
+
Its <ahref="https://docs.rs/anyhow/1.0/anyhow/trait.Context.html"><code>Context</code></a> trait can be used to add a description similar to our <code>CustomError</code> type.
672
+
Additionally, it keeps the original error,
673
+
so we get a “chain” of error messages pointing to the root cause.</p>
678
674
<p>Let’s first import the <code>anyhow</code> crate by adding
679
675
<code>anyhow = "1.0"</code> to the <code>[dependencies]</code> section
0 commit comments