rebuild pages at f15fe19

This commit is contained in:
Steve Klabnik
2016-12-29 01:46:32 +00:00
parent b44aad1247
commit 3bbf16da86
6 changed files with 1154 additions and 622 deletions
+19 -21
View File
@@ -68,27 +68,25 @@
<div id="content" class="content">
<h1>Error Handling</h1>
<p>Rust's focus on reliability extends to the area of error handling. Errors are a
fact of life in software, so Rust has a number of features that you can use to
handle situations in which something bad happens. In many cases, Rust requires
you to acknowledge the possibility of an error occurring and take some action
in that situation. This makes your program more robust by eliminating the
possibility of unexpected errors only being discovered after you've deployed
your code to production.</p>
<p>Rust groups errors into two major kinds: errors that are <em>recoverable</em>, and
errors that are <em>unrecoverable</em>. Recoverable errors are problems like a file not
being found, where it's usually reasonable to report that problem to the user
and retry the operation. Unrecoverable errors are problems like trying to
access a location beyond the end of an array, and these are always symptoms of
bugs.</p>
<p>Most languages do not distinguish between the two kinds of errors, so they
handle both kinds in the same way using mechanisms like exceptions. Rust
doesn't have exceptions. Instead, it has the value <code>Result&lt;T, E&gt;</code> to return in
the case of recoverable errors and the <code>panic!</code> macro that stops execution when
it encounters unrecoverable errors. This chapter will cover the more
straightforward case of calling <code>panic!</code> first. Then, we'll talk about
returning <code>Result&lt;T, E&gt;</code> values and calling functions that return <code>Result&lt;T, E&gt;</code>. Finally, we'll discuss considerations to take into account when deciding
whether to try to recover from an error or to stop execution.</p>
<p>Rust's commitment to reliability extends to error handling. Errors are a fact
of life in software, so Rust has a number of features for handling situations
in which something goes wrong. In many cases, Rust will require you to
acknowledge the possibility of an error occurring and take some action before
your code will compile. This makes your program more robust by ensuring that you
won't only discover errors after you've deployed your code to production.</p>
<p>Rust groups errors into two major categories: <em>recoverable</em> and <em>unrecoverable</em>
errors. Recoverable errors are situations when it's usually reasonable to
report the problem to the user and retry the operation, like a file not being
found. Unrecoverable errors are always symptoms of bugs, like trying to access
a location beyond the end of an array.</p>
<p>Most languages don't distinguish between the two kinds of errors, and handle
both in the same way using mechanisms like exceptions. Rust doesn't have
exceptions. Instead, it has the value <code>Result&lt;T, E&gt;</code> for recoverable errors and
the <code>panic!</code> macro that stops execution when it encounters unrecoverable
errors. This chapter will cover calling <code>panic!</code> first, then talk about
returning <code>Result&lt;T, E&gt;</code> values. Finally, we'll discuss considerations to take
into account when deciding whether to try to recover from an error or to stop
execution.</p>
</div>
+59 -35
View File
@@ -69,25 +69,29 @@
<div id="content" class="content">
<h2>Unrecoverable Errors with <code>panic!</code></h2>
<p>Sometimes, bad things happen, and there's nothing that you can do about it. For
these cases, Rust has a macro, <code>panic!</code>. When this macro executes, your program
will print a failure message, unwind and clean up the stack, and then quit. The
most common reason for this is when a bug of some kind has been detected, and
it's not clear how to handle the error.</p>
these cases, Rust has the <code>panic!</code> macro. When this macro executes, your
program will print a failure message, unwind and clean up the stack, and then
quit. The most common situation this occurs in is when a bug of some kind has
been detected and it's not clear to the programmer how to handle the error.</p>
<!-- PROD: START BOX -->
<blockquote>
<h4>Unwinding</h4>
<p>By default, when a <code>panic!</code> happens in Rust, the program starts
<p>By default, when a <code>panic!</code> occurs, the program starts
<em>unwinding</em>, which means Rust walks back up the stack and cleans up the data
from each function it encounters. Doing that walking and cleanup is a lot of
from each function it encounters, but this walking and cleanup is a lot of
work. The alternative is to immediately <code>abort</code>, which ends the program
without cleaning up. Memory that the program was using will need to be cleaned
up by the operating system. If you're in a situation where you need to make
the resulting binary as small as possible, you can switch from unwinding on
panic to aborting on panic by adding <code>panic = 'abort'</code> to the appropriate
<code>[profile]</code> sections in your <em>Cargo.toml</em>.</p>
without cleaning up. Memory that the program was using will then need to be
cleaned up by the operating system. If in your program you need to make
the resulting binary as small as possible, you can switch from unwinding to
aborting on panic by adding <code>panic = 'abort'</code> to the appropriate <code>[profile]</code>
sections in your <code>Cargo.toml</code>. For example, if you want to abort on panic in
release mode:</p>
<pre><code class="language-toml">[profile.release]
panic = 'abort'
</code></pre>
</blockquote>
<!-- PROD: END BOX -->
<p>Let's try out calling <code>panic!()</code> with a simple program:</p>
<p>Let's try calling <code>panic!()</code> with a simple program:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<pre><code class="language-rust,should_panic">fn main() {
panic!(&quot;crash and burn&quot;);
@@ -102,12 +106,20 @@ thread 'main' panicked at 'crash and burn', src/main.rs:2
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/panic` (exit code: 101)
</code></pre>
<p>There are three lines of error message here. The first line shows our panic
message and the place in our source code where the panic occurred:
<em>src/main.rs</em>, line two.</p>
<p>But that only shows us the exact line that called <code>panic!</code>. That's not always
useful. Let's look at another example to see what it's like when a <code>panic!</code>
call comes from code we call instead of from our code directly:</p>
<p>The last three lines contain the error message caused by the call to <code>panic!</code>.
The first line shows our panic message and the place in our source code where
the panic occurred: <code>src/main.rs:2</code> indicates that it's the second like of our
<em>main.rs</em> file.</p>
<p>In this case, the line indicated is part of our code, and if we go to that line
we see the <code>panic!</code> macro call. In other cases, the <code>panic!</code> call might be in
code that our code calls. The filename and line number reported by the error
message will be someone else's code where the <code>panic!</code> macro is called, not the
line of our code that eventually led to the <code>panic!</code>. We can use the backtrace
of the functions the <code>panic!</code> call came from to figure this out.</p>
<h3>Using a <code>panic!</code> Backtrace</h3>
<p>Let's look at another example to see what it's like when a <code>panic!</code> call comes
from a library because of a bug in our code instead of from our code calling
the macro directly:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<pre><code class="language-rust,should_panic">fn main() {
let v = vec![1, 2, 3];
@@ -117,8 +129,8 @@ call comes from code we call instead of from our code directly:</p>
</code></pre>
<p>We're attempting to access the hundredth element of our vector, but it only has
three elements. In this situation, Rust will panic. Using <code>[]</code> is supposed to
return an element. If you pass <code>[]</code> an invalid index, though, there's no
element that Rust could return here that would be correct.</p>
return an element, but if you pass an invalid index, there's no element that
Rust could return here that would be correct.</p>
<p>Other languages like C will attempt to give you exactly what you asked for in
this situation, even though it isn't what you want: you'll get whatever is at
the location in memory that would correspond to that element in the vector,
@@ -128,7 +140,7 @@ manipulate the index in such a way as to read data they shouldn't be allowed to
that is stored after the array.</p>
<p>In order to protect your program from this sort of vulnerability, if you try to
read an element at an index that doesn't exist, Rust will stop execution and
refuse to continue with an invalid value. Let's try it and see:</p>
refuse to continue. Let's try it and see:</p>
<pre><code class="language-text">$ cargo run
Compiling panic v0.1.0 (file:///projects/panic)
Finished debug [unoptimized + debuginfo] target(s) in 0.27 secs
@@ -139,12 +151,12 @@ note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/panic` (exit code: 101)
</code></pre>
<p>This points at a file we didn't write, <em>../src/libcollections/vec.rs</em>. That's
the implementation of <code>Vec&lt;T&gt;</code> in the standard library. While it's easy to see
in this short program where the error was, it would be nicer if we could have
Rust tell us what line in our program caused the error.</p>
<p>That's what the next line, the <code>note</code> is about. If we set the <code>RUST_BACKTRACE</code>
environment variable, we'll get a backtrace of exactly how the error happened.
Let's try that. Listing 9-1 shows the output:</p>
the implementation of <code>Vec&lt;T&gt;</code> in the standard library. The code that gets run
when we use <code>[]</code> on our vector <code>v</code> is in <em>../src/libcollections/vec.rs</em>, and
that is where the <code>panic!</code> is actually happening.</p>
<p>The next <code>note</code> line tells us that we can set the <code>RUST_BACKTRACE</code> environment
variable to get a backtrace of exactly what happened to cause the error. Let's
try that. Listing 9-1 shows the output:</p>
<figure>
<pre><code class="language-text">$ RUST_BACKTRACE=1 cargo run
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
@@ -184,14 +196,26 @@ the environment variable <code>RUST_BACKTRACE</code> is set</p>
</figcaption>
</figure>
<p>That's a lot of output! Line 11 of the backtrace points to the line in our
project causing the problem: <em>src/main.rs</em> line four. The key to reading the
backtrace is to start from the top and read until we see files that we wrote:
that's where the problem originated. If we didn't want our program to panic
here, this line is where we would start investigating in order to figure out
how we got to this location with values that caused the panic.</p>
<p>Now that we've covered how to <code>panic!</code> to stop our code's execution and how to
debug a <code>panic!</code>, let's look at how to instead return and use recoverable
errors with <code>Result</code>.</p>
project causing the problem: <code>src/main.rs</code>, line four. A backtrace is a list of
all the functions that have been called to get to this point. Backtraces in
Rust work like they do in other languages: the key to reading the backtrace is
to start from the top and read until you see files you wrote. That's the spot
where the problem originated. The lines above the lines mentioning your files
are code that your code called; the lines below are code that called your code.
These lines might include core Rust code, standard library code, or crates that
you're using.</p>
<p>If we don't want our program to panic, the location pointed to by the first
line mentioning a file we wrote is where we should start investigating in order
to figure out how we got to this location with values that caused the panic. In
our example where we deliberately wrote code that would panic in order to
demonstrate how to use backtraces, the way to fix the panic is to not try to
request an element at index 100 from a vector that only contains three items.
When your code panics in the future, you'll need to figure out for your
particular case what action the code is taking with what values that causes the
panic and what the code should do instead.</p>
<p>We'll come back to <code>panic!</code> and when we should and should not use these methods
later in the chapter. Next, we'll now look at how to recover from an error with
<code>Result</code>.</p>
</div>
+328 -168
View File
@@ -68,27 +68,40 @@
<div id="content" class="content">
<h2>Recoverable Errors with <code>Result</code></h2>
<p>Most errors aren't so dire. Sometimes, when a function fails, it's for a reason
that we can easily interpret and respond to. As an example, maybe we are making
a request to a website, but it's down for maintenance. In this situation, we'd
like to wait and then try again. Terminating our process isn't the right thing
to do here.</p>
<p>In these cases, Rust's standard library provides an <code>enum</code> to use as the return
type of the function:</p>
<p>Most errors aren't serious enough to require the program to stop entirely.
Sometimes, when a function fails, it's for a reason that we can easily
interpret and respond to. For example, if we try to open a file and that
operation fails because the file doesn't exist, we might want to create the
file instead of terminating the process.</p>
<p>Recall from Chapter 2 the section on &quot;Handling Potential Failure with the
<code>Result</code> Type&quot; that the <code>Result</code> enum is defined as having two variants, <code>Ok</code>
and <code>Err</code>, as follows:</p>
<pre><code class="language-rust">enum Result&lt;T, E&gt; {
Ok(T),
Err(E),
}
</code></pre>
<p>The <code>Ok</code> variant indicates a successful result, and <code>Err</code> indicates an
unsuccessful result. These two variants each contain one thing: in <code>Ok</code>'s case,
it's the successful return value. With <code>Err</code>, it's some value that represents
the error. The <code>T</code> and <code>E</code> are generic type parameters; we'll go into generics
in more detail in Chapter 10. What you need to know for right now is that the
<code>Result</code> type is defined such that it can have the same behavior for any type
<code>T</code> that is what we want to return in the success case, and any type <code>E</code> that
is what we want to return in the error case.</p>
<p>Listing 9-2 shows an example of something that might fail: opening a file.</p>
<!-- Would it make sense for this to be something like:
<pre><code>enum Result&lt;T, E&gt; {
Ok(T) =&gt; successful_result,
Err(E) =&gt; error,
}
</code></pre>
<p>instead? Then you could concretely explain the returned result.
--&gt;</p>
<!-- This notation looks similar to a `match`, but it's not a `match`, so we
think this would be confusing. We've tried to clarify better in the text.
/Carol -->
<p>The <code>T</code> and <code>E</code> are generic type parameters; we'll go into generics in more
detail in Chapter 10. What you need to know right now is that <code>T</code> represents
the type of the value that will be returned in a success case within the <code>Ok</code>
variant, and <code>E</code> represents the type of the error that will be returned in a
failure case within the <code>Err</code> variant. Because <code>Result</code> has these generic type
parameters, we can use the <code>Result</code> type and the functions that the standard
library has defined on it in many different situations where the successful
value and error value we want to return may differ.</p>
<p>Let's call a function that returns a <code>Result</code> value because the function could
fail: opening a file, shown in Listing 9-2.</p>
<figure>
<span class="filename">Filename: src/main.rs</span>
<pre><code class="language-rust">use std::fs::File;
@@ -101,12 +114,54 @@ fn main() {
<p>Listing 9-2: Opening a file</p>
</figcaption>
</figure>
<p>The type of <code>f</code> in this example is a <code>Result</code>, because there are many ways in
which opening a file can fail. For example, unless we created <code>hello.txt</code>, this
file does not yet exist. Before we can do anything with our <code>File</code>, we need to
extract it out of the result. Listing 9-3 shows one way to handle the <code>Result</code>
with a basic tool: the <code>match</code> expression that we learned about in Chapter 6.</p>
<!-- I'll ghost everything except the match statement lines in the libreoffice file /Carol -->
<p>How do we know <code>File::open</code> returns a <code>Result</code>? We could look at the standard
library API documentation. We could ask the compiler! If we give <code>f</code> a type
annotation of some type that we know the return type of the function is <em>not</em>,
then we try to compile the code, the compiler will tell us that the types don't
match. The error message will then tell us what the type of <code>f</code> <em>is</em>! Let's try
it: we know that the return type of <code>File::open</code> isn't of type <code>u32</code>, so let's
change the <code>let f</code> statement to:</p>
<pre><code class="language-rust,ignore">let f: u32 = File::open(&quot;hello.txt&quot;);
</code></pre>
<p>Attempting to compile now gives us:</p>
<pre><code class="language-text">error[E0308]: mismatched types
--&gt; src/main.rs:4:18
|
4 | let f: u32 = File::open(&quot;hello.txt&quot;);
| ^^^^^^^^^^^^^^^^^^^^^^^ expected u32, found enum `std::result::Result`
|
= note: expected type `u32`
= note: found type `std::result::Result&lt;std::fs::File, std::io::Error&gt;`
</code></pre>
<p>This tells us the return type of the <code>File::open</code> function is a <code>Result&lt;T, E&gt;</code>.
The generic parameter <code>T</code> has been filled in here with the type of the success
value, <code>std::fs::File</code>, which is a file handle. The type of <code>E</code> used in the
error value is <code>std::io::Error</code>.</p>
<p>This return type means the call to <code>File::open</code> might succeed and return to us
a file handle that we can read from or write to. The function call also might
fail: for example, the file might not exist, or we might not have permission to
access the file. The <code>File::open</code> function needs to have a way to tell us
whether it succeeded or failed, and at the same time give us either the file
handle or error information. This information is exactly what the <code>Result</code> enum
conveys.</p>
<p>In the case where <code>File::open</code> succeeds, the value we will have in the variable
<code>f</code> will be an instance of <code>Ok</code> that contains a file handle. In the case where
it fails, the value in <code>f</code> will be an instance of <code>Err</code> that contains more
information about the kind of error that happened.</p>
<!--Can you say explicitly why there being many ways things can fail means we
use the result type? Also, are we importing the File type from the standard
crate here? That seems worth mentioning. -->
<!-- We think it would be repetitive to point out every example that imports a
type from the standard library. We're past the Modules Chapter 7 "Importing
Names With Use" section that explains the concept in depth, as well as multiple
examples in the Hash maps section of Chapter 8 that show how and why to import
types from the standard library. /Carol -->
<p>We need to add to the code from Listing 9-2 to take different actions depending
on the value <code>File::open</code> returned. Listing 9-3 shows one way to handle the
<code>Result</code> with a basic tool: the <code>match</code> expression that we learned about in
Chapter 6.</p>
<!-- I'll ghost everything except the match statement lines in the libreoffice
file /Carol -->
<figure>
<span class="filename">Filename: src/main.rs</span>
<pre><code class="language-rust,should_panic">use std::fs::File;
@@ -126,25 +181,41 @@ error),
might have</p>
</figcaption>
</figure>
<p>If we see an <code>Ok</code>, we can return the inner <code>file</code> out of the <code>Ok</code> variant. If
we see <code>Err</code>, we have to decide what to do with it. The simplest thing is to
turn our error into a <code>panic!</code> instead, by calling the macro. And since we
haven't created that file yet, we'll see a message indicating as such when we
print the error value:</p>
<!-- So we don't need the Result keyword in this code example? And what is the
{:?} syntax, can you include a line about that? -->
<!-- We've added an explanation that Result is like Option in that it's
imported into the prelude, which the reader should be familiar with. We
explained the {:?} syntax in Structs, chapter 5, in the section "Adding Useful
Functionality with Derived Traits". It's the debug format. Having to re-explain
multiple concepts that are not the primary focus of this example really
obscures the point of the section. /Carol -->
<p>Note that, like the <code>Option</code> enum, the <code>Result</code> enum and its variants have been
imported in the prelude, so we don't need to specify <code>Result::</code> before the <code>Ok</code>
and <code>Err</code> variants in the <code>match</code> arms.</p>
<p>Here we tell Rust that when the result is <code>Ok</code>, return the inner <code>file</code> value
out of the <code>Ok</code> variant, and we then assign that file handle value to the
variable <code>f</code>. After the <code>match</code>, we can then use the file handle for reading or
writing.</p>
<p>The other arm of the <code>match</code> handles the case where we get an <code>Err</code> value from
<code>File::open</code>. In this example, we've chosen to call the <code>panic!</code> macro. If
there's no file named <code>hello.txt</code> in our current directory and we run this
code, we'll see the following output from the <code>panic!</code> macro:</p>
<pre><code class="language-text">thread 'main' panicked at 'There was a problem opening the file: Error { repr:
Os { code: 2, message: &quot;No such file or directory&quot; } }', src/main.rs:8
</code></pre>
<!-- Do we have to manually print the error message, or does it show when we
run the program? -->
<!-- No, the `panic!` macro prints what we give to it, which we covered in the
section previous to this one. /Carol -->
<h3>Matching on Different Errors</h3>
<p>There are many reasons why opening a file might fail, and we may not want to
take the same actions to try to recover for all of them. For example, if the
file we're trying to open does not exist, we could choose to create it. If the
file exists but we don't have permission to read it, or any other error, we
still want to <code>panic!</code> in the same way as above and not create the file.</p>
<p>The <code>Err</code> type <code>File::open</code> returns is <a href="../std/io/struct.Error.html"><code>io::Error</code></a><!-- ignore -->,
which is a struct provided by the standard library. This struct has a method
<code>kind</code> that we can call to get an <a href="../std/io/enum.ErrorKind.html"><code>io::ErrorKind</code></a><!-- ignore -->
value that we can use to handle different causes of an <code>Err</code> returned from
<code>File::open</code> differently as in Listing 9-4:</p>
<p>The code in Listing 9-3 will <code>panic!</code> no matter the reason that <code>File::open</code>
failed. What we'd really like to do instead is take different actions for
different failure reasons: if <code>File::open</code> failed because the file doesn't
exist, we want to create the file and return the handle to the new file. If
<code>File::open</code> failed for any other reason, for example because we didn't have
permission to open the file, we still want to <code>panic!</code> in the same way as we
did in Listing 9-3. Let's look at Listing 9-4, which adds another arm to the
<code>match</code>:</p>
<figure>
<span class="filename">Filename: src/main.rs</span>
<pre><code class="language-rust,ignore">use std::fs::File;
@@ -171,79 +242,117 @@ error),
</figcaption>
</figure>
<!-- I will add ghosting and wingdings here in libreoffice /Carol -->
<p>This example uses a <em>match guard</em> with the second arm's pattern to add a
condition that further refines the pattern. The <code>ref</code> in the pattern is needed
so that the <code>error</code> is not moved into the guard condition. The condition we
want to check is that the value <code>error.kind()</code> returns is the <code>NotFound</code>
variant of the <code>ErrorKind</code> enum. Note that <code>File::create</code> could also fail, so
we need to add an inner <code>match</code> statement as well! The last arm of the outer
<code>match</code> stays the same to panic on any error besides the file not being found.</p>
<p>The type of the value that <code>File::open</code> returns inside the <code>Err</code> variant is
<code>io::Error</code>, which is a struct provided by the standard library. This struct
has a method <code>kind</code> that we can call to get an <code>io::ErrorKind</code> value.
<code>io::ErrorKind</code> is an enum provided by the standard library that has variants
representing the different kinds of errors that might result from an <code>io</code>
operation. The variant we're interested in is <code>ErrorKind::NotFound</code>, which
indicates the file we're trying to open doesn't exist yet.</p>
<p>The condition <code>if error.kind() == ErrorKind::NotFound</code> is called a <em>match
guard</em>: it's an extra condition on a <code>match</code> arm that further refines the arm's
pattern. This condition must be true in order for that arm's code to get run;
otherwise, the pattern matching will move on to consider the next arm in the
<code>match</code>. The <code>ref</code> in the pattern is needed so that the <code>error</code> is not moved
into the guard condition but is merely referenced by it. The reason <code>ref</code> is
used to take a reference in a pattern instead of <code>&amp;</code> will be covered in detail
in Chapter XX. In short, in the context of a pattern, <code>&amp;</code> matches a reference
and give us its value, but <code>ref</code> matches a value and gives us a reference to it.</p>
<p>The condition we want to check in the match guard is whether the value returned
by <code>error.kind()</code> is the <code>NotFound</code> variant of the <code>ErrorKind</code> enum. If it is,
we try to create the file with 'File::create'. However, since <code>File::create</code>
could also fail, we need to add an inner <code>match</code> statement as well! When the
file can't be opened, a different error message will be printed. The last arm
of the outer <code>match</code> stays the same so that the program panics on any error
besides the missing file error.</p>
<h3>Shortcuts for Panic on Error: <code>unwrap</code> and <code>expect</code></h3>
<p>Using <code>match</code> works okay but can be a bit verbose, and it doesn't always
<p>Using <code>match</code> works well enough, but it can be a bit verbose and doesn't always
communicate intent well. The <code>Result&lt;T, E&gt;</code> type has many helper methods
defined on it to do various things. &quot;Panic on an error result&quot; is one of those
methods, and it's called <code>unwrap()</code>:</p>
defined on it to do various things. One of those methods, called <code>unwrap</code>, is
a shortcut method that is implemented just like the <code>match</code> statement we wrote
in Listing 9-3. If the <code>Result</code> value is the <code>Ok</code> variant, <code>unwrap</code> will return
the value inside the <code>Ok</code>. If the <code>Result</code> is the <code>Err</code> variant, <code>unwrap</code> will
call the <code>panic!</code> macro for us.</p>
<!-- Can you explain a bit more what unwrap() does---you mean every time we
cause a panic it calls the unwrap method? -->
<!-- I'm not sure how the conclusion "every time we cause a panic it calls the
unwrap method" follows from the text that was here, but I've tried to reword.
Please let us know what part of the text specifically is implying that here so
that we can be sure that we've fixed it. /Carol -->
<!-- I'll ghost everything except `unwrap()` in the libreoffice file /Carol -->
<p><span class="filename">Filename: src/main.rs</span></p>
<pre><code class="language-rust,should_panic">use std::fs::File;
fn main() {
let f = File::open(&quot;hello.txt&quot;).unwrap();
}
</code></pre>
<p>This has similar behavior as the example using <code>match</code> in Listing 9-3: If the
call to <code>open()</code> returns <code>Ok</code>, return the value inside. If it's an <code>Err</code>, panic.</p>
<p>There's also another method that is similar to <code>unwrap()</code>, but lets us choose
the error message: <code>expect()</code>. Using <code>expect()</code> instead of <code>unwrap()</code> and
<!-- Can you talk ore about the syntax here, how it differs? It looks like
there aren't generics here for T and E. How is this still related to Result? -->
<!-- I'm not sure how to make this clearer. We're chaining the method call onto
the return value of the `File::open` function, which hasn't changed. The reader
should understand method calls by now. T and E are part of the *definition* of
the Result type, since Listing 9-2 we've been talking about *using* a Result
instance. Listings 9-2, 9-3, and 9-4 don't contain T and E either, so I'm not
sure why it's confusing that this code doesn't contain T and E. /Carol -->
<p>If we run this code without a <em>hello.txt</em> file, we'll see an error message from
the <code>panic</code> call that the <code>unwrap</code> method makes:</p>
<pre><code class="language-text">thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error {
repr: Os { code: 2, message: &quot;No such file or directory&quot; } }',
../src/libcore/result.rs:837
</code></pre>
<p>There's another method similar to <code>unwrap</code> that lets us also choose the
<code>panic!</code> error message: <code>expect</code>. Using <code>expect</code> instead of <code>unwrap</code> and
providing good error messages can convey your intent and make tracking down the
source of a panic easier. <code>expect()</code> looks like this:</p>
<!-- I'll ghost everything except `expect()` in the libreoffice file /Carol -->
<p><span class="filename">Filename: src/main.rs</span></p>
source of a panic easier. The syntax of<code>expect</code> looks like this:</p>
<!-- I'll ghost everything except `expect(...)` in the libreoffice file /Carol -->
<pre><code class="language-rust,should_panic">use std::fs::File;
fn main() {
let f = File::open(&quot;hello.txt&quot;).expect(&quot;Failed to open hello.txt.&quot;);
let f = File::open(&quot;hello.txt&quot;).expect(&quot;Failed to open hello.txt&quot;);
}
</code></pre>
<p>This isn't the only way to deal with errors, however. This entire section is
supposed to be about recovering from errors, but we've gone back to panic. This
observation gets at an underlying truth: you can easily turn a recoverable
error into an unrecoverable one with <code>unwrap()</code> or <code>expect()</code>, but you can't
turn an unrecoverable <code>panic!</code> into a recoverable one. This is why good Rust
code chooses to make errors recoverable: you give your caller choices.</p>
<p>The Rust community has a love/hate relationship with <code>unwrap()</code> and <code>expect()</code>.
They're very handy when prototyping, before you're ready to decide how to
handle errors, and in that case they leave clear markers to look for when you
are ready to make your program more robust. They're useful in tests since they
will cause the test to fail if there's an error any place you call them. In
examples, you might not want to muddy the code with proper error handling. But
if you use them in a library, mis-using your library can cause other people's
programs to halt unexpectedly, and that's not very user-friendly.</p>
<p>Another time it's appropriate to call <code>unwrap</code> is when we have some other logic
that ensures the <code>Result</code> will have an <code>Ok</code> value, but the logic isn't
something the compiler understands. If you can ensure by manually inspecting
the code that you'll never have an <code>Err</code> variant, it is perfectly acceptable to
call <code>unwrap</code>. Here's an example:</p>
<pre><code class="language-rust">use std::net::IpAddr;
let home = &quot;127.0.0.1&quot;.parse::&lt;IpAddr&gt;().unwrap();
<p>We use <code>expect</code> in the same way as <code>unwrap</code>: to return the file handle or call
the <code>panic!</code> macro. The error message that <code>expect</code> uses in its call to
<code>panic!</code> will be the parameter that we pass to <code>expect</code> instead of the default
<code>panic!</code> message that <code>unwrap</code> uses. Here's what it looks like:</p>
<pre><code class="language-text">thread 'main' panicked at 'Failed to open hello.txt: Error { repr: Os { code:
2, message: &quot;No such file or directory&quot; } }', ../src/libcore/result.rs:837
</code></pre>
<p>We're creating an <code>IpAddr</code> instance by parsing a hardcoded string. We can see
that <code>&quot;127.0.0.1&quot;</code> is a valid IP address, so it's acceptable to use <code>unwrap</code>
here. If we got the IP address string from a user of our program instead of
hardcoding this value, we'd definitely want to handle the <code>Result</code> in a more
robust way instead.</p>
<h3>Propagating errors with <code>try!</code> or <code>?</code></h3>
<p>When writing a function, if you don't want to handle the error where you are,
you can return the error to the calling function. For example, Listing 9-5
shows a function that reads a username from a file. If the file doesn't exist
or can't be read, this function will return those errors to the code that
called this function:</p>
<!-- I added the above paragraph, can you review it and correct it as
necessary? So this is like what we did in Listing 9-3?-->
<!-- Yes, the implementations for both `unwrap` and `expect` are similar to 9-3,
which we want to show so that the reader knows they don't have to write out all
of 9-3 every time they have a `Result` value. Does this comment mean your
earlier comments in this section are moot? /Carol -->
<!-- Is panic used for both types of errors? The introduction makes it seem as
though it's only for unrecoverable errors -->
<!-- When you call panic, you are causing the program to crash and therefore
creating an unrecoverable error. You can choose to do that at any time, even
when there are *no* errors. There's nothing that prevents you from calling
`panic!` inappropriately, which is why the "to panic or not to panic" section
goes over the criteria the reader should use to decide if they're in a
situation that's recoverable or not. I've actually moved the text that was here
into that section to keep that whole discussion together. /Carol
-->
<h3>Propagating Errors</h3>
<p>When writing a function whose implementation calls something that might fail,
instead of handling the error within this function, you can choose to let your
caller know about the error so they can decide what to do. This is known as
<em>propagating</em> the error, and gives more control to the calling code where there
might be more information or logic that dictates how the error should be
handled than what you have available in the context of your code.</p>
<!-- What's the benefit/result of returning the error to the code that called
the function, besides putting off handling it---can you lay that out? -->
<!-- We're giving control/decision making ability to the code that's calling
our code. I've tried to be more explicit here; please let me know what could be
improved if it's still not clear. /Carol -->
<p>For example, Listing 9-5 shows a function that reads a username from a file. If
the file doesn't exist or can't be read, this function will return those errors
to the code that called this function:</p>
<figure>
<pre><code class="language-rust"># use std::fs::File;
# use std::io;
# use std::io::Read;
#
<pre><code class="language-rust">use std::io;
use std::fs::File;
fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
let f = File::open(&quot;hello.txt&quot;);
@@ -264,46 +373,55 @@ fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
<p>Listing 9-5: A function that returns errors to the calling code using <code>match</code></p>
</figcaption>
</figure>
<p>Since the <code>Result</code> type has two type parameters, we need to include them both
in our function signature. In this case, <code>File::open</code> and <code>read_to_string</code>
return <code>std::io::Error</code> as the value inside the <code>Err</code> variant, so we will also
use it as our error type. If this function succeeds, we want to return the
username as a <code>String</code> inside the <code>Ok</code> variant, so that is our success type.</p>
<p>This is a very common way of handling errors: propagate them upward until
you're ready to deal with them. This pattern is so common in Rust that there is
a macro for it, <code>try!</code>, and as of Rust 1.14 <!-- 1.14 has not been released as
a stable version yet, but that's the version the question mark operator will be
released in /Carol -->, dedicated syntax for it: the question mark
operator. We could have written the code in Listing 9-5 using the <code>try!</code> macro,
as in Listing 9-6, and it would have the same functionality as the <code>match</code>
expressions:</p>
<!-- I'll ghost everything except the calls to `try!` in the libreoffice file
/Carol -->
<p>Let's look at the return type of the function first: <code>Result&lt;String, io::Error&gt;</code>. This means that the function is returning a value of the type
<code>Result&lt;T, E&gt;</code> where the generic parameter <code>T</code> has been filled in with the
concrete type <code>String</code>, and the generic type <code>E</code> has been filled in with the
concrete type <code>io::Error</code>. If this function succeeds without any problems, the
caller of this function will receive an <code>Ok</code> value that holds a <code>String</code> -- the
username that this function read from the file. If this function encounters any
problems, the caller of this function will receive an <code>Err</code> value that holds an
instance of <code>io::Error</code> that contains more information about what the problems
were. We chose <code>io::Error</code> as the return type of this function because that
happens to be the type of the error value returned from both of the operations
we're calling in this function's body that might fail: the <code>File::open</code>
function and the <code>read_to_string</code> method.</p>
<p>The body of the function starts by calling the <code>File::open</code> function. Then we
handle the <code>Result</code> value returned with a <code>match</code> similar to the <code>match</code> in
Listing 9-3, only instead of calling <code>panic!</code> in the <code>Err</code> case, we return
early from this function and pass the error value from <code>File::open</code> back to the
caller as this function's error value. If <code>File::open</code> succeeds, we store the
file handle in the variable <code>f</code> and continue.</p>
<p>Then we create a new <code>String</code> in variable <code>s</code> and call the <code>read_to_string</code>
method on the file handle in <code>f</code> in order to read the contents of the file into
<code>s</code>. The <code>read_to_string</code> method also returns a <code>Result</code> because it might fail,
even though <code>File::open</code> succeeded. So we need another <code>match</code> to handle that
<code>Result</code>: if <code>read_to_string</code> succeeds, then our function has succeeded, and we
return the username from the file that's now in <code>s</code> wrapped in an <code>Ok</code>. If
<code>read_to_string</code> fails, we return the error value in the same way that we
returned the error value in the <code>match</code> that handled the return value of
<code>File::open</code>. We don't need to explicitly say <code>return</code>, however, since this is
the last expression in the function.</p>
<p>The code that calls this code will then handle getting either an <code>Ok</code> value
that contains a username or an <code>Err</code> value that contains an <code>io::Error</code>. We
don't know what the caller will do with those values. If they get an <code>Err</code>
value, they could choose to call <code>panic!</code> and crash their program, use a
default username, or look up the username from somewhere other than a file, for
example. We don't have enough information on what the caller is actually trying
to do, so we propagate all the success or error information upwards for them to
handle as they see fit.</p>
<p>This pattern of propagating errors is so common in Rust that there is dedicated
syntax to make this easier: <code>?</code>.</p>
<h3>A Shortcut for Propagating Errors: <code>?</code></h3>
<!-- The `?` ended up stabilizing in 1.13 and is quickly becoming preferred over
`try!`, so we decided to only cover `?`. /Carol -->
<p>Listing 9-6 shows an implementation of <code>read_username_from_file</code> that has the
same functionality as it had in Listing 9-5, but this implementation uses the
question mark:</p>
<!-- I'll ghost everything except the question mark in libreoffice. /Carol -->
<figure>
<pre><code class="language-rust"># use std::fs::File;
# use std::io;
# use std::io::Read;
#
fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
let mut f = try!(File::open(&quot;hello.txt&quot;));
let mut s = String::new();
<pre><code class="language-rust">use std::io;
use std::fs::File;
try!(f.read_to_string(&amp;mut s));
Ok(s)
}
</code></pre>
<figcaption>
<p>Listing 9-6: A function that returns errors to the calling code using <code>try!</code></p>
</figcaption>
</figure>
<p>Or as in Listing 9-7, which uses the question mark operator:</p>
<figure>
<pre><code class="language-rust"># fn main() {}
# use std::fs::File;
# use std::io;
# use std::io::Read;
#
fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
let mut f = File::open(&quot;hello.txt&quot;)?;
let mut s = String::new();
@@ -312,22 +430,32 @@ fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
}
</code></pre>
<figcaption>
<p>Listing 9-7: A function that returns errors to the calling code using <code>?</code></p>
<p>Listing 9-6: A function that returns errors to the calling code using <code>?</code></p>
</figcaption>
</figure>
<p>The <code>?</code> operator at the end of the <code>open</code> call does the same thing as the
example that uses <code>match</code> and the example that uses the <code>try!</code> macro: It will
return the value inside an <code>Ok</code> to the binding <code>f</code>, but will return early out
of the whole function and give any <code>Err</code> value we get to our caller. The same
thing applies to the <code>?</code> at the end of the <code>read_to_string</code> call.</p>
<p>The advantage of using the question mark operator over the <code>try!</code> macro is the
question mark operator permits chaining. We could further shorten this code
by instead doing:</p>
<pre><code class="language-rust"># fn main() {}
# use std::fs::File;
# use std::io;
# use std::io::Read;
#
<!-- Below, are we talking about what just the ? operator does, or what the
program with the ? operator does? -->
<!-- I'm not sure what the difference is. We're talking about what the ? does
in the context of this program... /Carol -->
<p>The <code>?</code> placed after a <code>Result</code> value is defined to work the exact same way as
the<code>match</code> expressions we defined to handle the <code>Result</code> values in Listing 9-5.
If the value of the <code>Result</code> is an <code>Ok</code>, the value inside the <code>Ok</code> will get
returned from this expression and the program will continue. If the value is an
<code>Err</code>, the value inside the <code>Err</code> will be returned from the whole function as
if we had used the <code>return</code> keyword so that the error value gets propagated to
the caller.</p>
<p>In the context of Listing 9-6, the <code>?</code> at the end of the <code>File::open</code> call will
return the value inside an <code>Ok</code> to the binding <code>f</code>. If an error occurs, <code>?</code>
will return early out of the whole function and give any <code>Err</code> value to our
caller. The same thing applies to the <code>?</code> at the end of the <code>read_to_string</code>
call.</p>
<p>The <code>?</code> eliminates a lot of boilerplate and makes this function's
implementation simpler. We could even shorten this code further by chaining
method calls immediately after the <code>?</code>:</p>
<pre><code class="language-rust">use std::io;
use std::io::Read;
use std::fs::File;
fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
let mut s = String::new();
@@ -336,39 +464,71 @@ fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {
Ok(s)
}
</code></pre>
<p>Much nicer, right? The <code>try!</code> macro and the <code>?</code> operator make propagating
errors upwards much more ergonomic. There's one catch though: they can only be
used in functions that return a <code>Result</code>, since they expand to the same <code>match</code>
expression we saw above that had a potential early return of an <code>Err</code> value.
Let's look at what happens if we try to use <code>try!</code> in the <code>main</code> function,
which you'll recall has a return type of <code>()</code>:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<pre><code class="language-rust,ignore"># use std::fs::File;
fn main() {
let f = try!(File::open(&quot;hello.txt&quot;));
<!-- Can you explain what is happening in this code and how it differs? -->
<!-- I've tried to make it even clearer that the functionality does NOT differ
/Carol -->
<p>We've moved the creation of the new <code>String</code> in <code>s</code> to the beginning of the
function; that part hasn't changed. Instead of creating a variable <code>f</code>, we've
chained the call to <code>read_to_string</code> directly onto the result of
<code>File::open(&quot;hello.txt&quot;)?</code>. We still have a <code>?</code> at the end of the
<code>read_to_string</code> call, and we still return an <code>Ok</code> value containing the
username in <code>s</code> when both <code>File::open</code> and <code>read_to_string</code> succeed rather than
returning errors. The functionality is again the same as in Listing 9-5 and
Listing 9-6, this is just a different, more ergonomic way to write it.</p>
<h4><code>?</code> Can Only Be Used in Functions That Return <code>Result</code></h4>
<!-- I think we need a new heading here, could you suggest something? I'm sure
there's a better way to phrase this!-->
<!-- I've tried, but I'm not really sure how to say it any more succinctly than
this, I'm not sure if it's better than what you suggested /Carol -->
<p>The <code>?</code> can only be used in functions that have a return type of <code>Result</code>,
since it is defined to work in exactly the same way as the <code>match</code> expression
we defined in Listing 9-5. The part of the <code>match</code> that requires a return type
of <code>Result</code> is <code>return Err(e)</code>, so the return type of the function must be a
<code>Result</code> to be compatible with this <code>return</code>.</p>
<!-- Which functions return a Result and how would the reader know? I'm also not
sure what you mean by "expand", that they have the same functionality (but
condensed!)? -->
<!-- You can tell what any function returns by looking at the return type
defined in the function signature, I'm not sure what part of Chapter 3 wasn't
clear enough to convey that. The reader should be comfortable with function
signatures by this point, and could also use the API docs to tell what a
function returns.
<p>I've reworded to remove the word expand, but yes, we meant &quot;functionally
equivalent to replacing it with the longer code&quot;</p>
<p>/Carol
--&gt;</p>
<p>Let's look at what happens if use <code>try!</code> in the <code>main</code> function, which you'll
recall has a return type of <code>()</code>:</p>
<pre><code class="language-rust,ignore">fn main() {
let f = File::open(&quot;hello.txt&quot;)?;
}
</code></pre>
<!-- NOTE: as of 2016-10-12, the error message when calling `?` in a function
that doesn't return a result is confusing. `try!` isn't as bad, so I'm using
that. When https://github.com/rust-lang/rust/issues/35946 is fixed, we can
switch this example to use `?`. /Carol -->
<!-- NOTE: as of 2016-12-21, the error message when calling `?` in a function
that doesn't return a result is STILL confusing. Since we want to only explain
`?` now, I've changed the example, but if you try running this code you WON'T
get the error message below.
<p>I'm bugging people to try and get
https://github.com/rust-lang/rust/issues/35946 fixed soon, hopefully before this
chapter gets through copy editing-- at that point I'll make sure to update this
error message. /Carol --&gt;</p>
<p>When we compile this, we get the following error message:</p>
<pre><code class="language-text">error[E0308]: mismatched types
<pre><code class="language-bash">error[E0308]: mismatched types
--&gt;
|
3 | let f = try!(File::open(&quot;hello.txt&quot;));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::result::Result`
3 | let f = File::open(&quot;hello.txt&quot;)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::result::Result`
|
= note: expected type `()`
= note: found type `std::result::Result&lt;_, _&gt;`
</code></pre>
<p>The mismatched types that this error is pointing out says the <code>main()</code> function
has a return type of <code>()</code>, but the <code>try!</code> macro might return a <code>Result</code>. So in
functions that don't return <code>Result</code>, when you call other functions that return
<code>Result</code>, you'll need to use a <code>match</code> or one of the methods on <code>Result</code> to
handle it instead of using <code>try!</code> or <code>?</code>.</p>
<p>This error is pointing out that we have mismatched types: the <code>main()</code> function
has a return type of <code>()</code>, but the <code>?</code> might return a <code>Result</code>. In functions
that don't return <code>Result</code>, when you call other functions that return <code>Result</code>,
you'll need to use a <code>match</code> or one of the <code>Result</code> methods to handle it,
instead of using <code>?</code> to potentially propagate the error to the caller.</p>
<p>Now that we've discussed the details of calling <code>panic!</code> or returning <code>Result</code>,
let's return to the topic of how to decide which is appropriate in which cases.</p>
let's return to the topic of how to decide which is appropriate to use in which
cases.</p>
</div>
+170 -86
View File
@@ -68,74 +68,137 @@
<div id="content" class="content">
<h2>To <code>panic!</code> or Not To <code>panic!</code></h2>
<p>So how do you decide when you should call <code>panic!</code> and when you should return
<code>Result</code>? A good default for a function that might fail is to return <code>Result</code>
since that gives the caller of your function the most flexibility.</p>
<p>But that answer is simplistic. There are cases where you might want to call
<code>panic!</code> in library code that have to do with Rust's quest for safety. Let's
look at some more nuanced guidelines.</p>
<p>So how do you decide when you should <code>panic!</code> and when you should return
<code>Result</code>? When code panics, there's no way to recover. You could choose to call
<code>panic!</code> for any error situation, whether there's a possible way to recover or
not, but then you're making the decision for your callers that a situation is
unrecoverable. When you choose to return a <code>Result</code> value, you give your caller
options, rather than making the decision for them. They could choose to attempt
to recover in a way that's appropriate for their situation, or they could
decide that actually, an <code>Err</code> value in this case is unrecoverable, so they can
call <code>panic!</code> and turn your recoverable error into an unrecoverable one.
Therefore, returning <code>Result</code> is a good default choice when you're defining a
function that might fail.</p>
<p>There are a few situations in which it's more appropriate to write code that
panics instead of returning a <code>Result</code>, but they are less common. Let's discuss
why it's appropriate to panic in examples, prototype code, and tests, then
situations where you as a human can know a method won't fail that the compiler
can't reason about, and conclude with some general guidelines on how to decide
whether to panic in library code.</p>
<h3>Examples, Prototype Code, and Tests: Perfectly Fine to Panic</h3>
<p>When you're writing an example to illustrate some concept, having robust error
handling code in the example as well can make the example less clear. In
examples, it's understood that a call to a method like <code>unwrap</code> that could
<code>panic!</code> is meant as a placeholder for the way that you'd actually like your
application to handle errors, which can differ based on what the rest of your
code is doing.</p>
<p>Similarly, the <code>unwrap</code> and <code>expect</code> methods are very handy when prototyping,
before you're ready to decide how to handle errors. They leave clear markers in
your code for when you're ready to make your program more robust.</p>
<p>If a method call fails in a test, we'd want the whole test to fail, even if that
method isn't the functionality under test. Because <code>panic!</code> is how a test gets
marked as a failure, calling <code>unwrap</code> or <code>expect</code> is exactly what makes sense to
do.</p>
<h3>Cases When You Have More Information Than The Compiler</h3>
<p>It would also be appropriate to call <code>unwrap</code> when you have some other logic
that ensures the <code>Result</code> will have an <code>Ok</code> value, but the logic isn't
something the compiler understands. You'll still have a <code>Result</code> value that you
need to handle: whatever operation you're calling still has the possibility of
failing in general, even though it's logically impossible in your particular
situation. If you can ensure by manually inspecting the code that you'll never
have an <code>Err</code> variant, it is perfectly acceptable to call <code>unwrap</code>. Here's an
example:</p>
<!-- If we know that there won't be an error, why do we still need to use
unwrap()? Can you clarify that in the text? -->
<!-- Because you still have to extract the value from the `Ok`; knowing there
won't be an error doesn't change the types. I've tried to clarify in the
paragraph above and again below. /Carol-->
<pre><code class="language-rust">use std::net::IpAddr;
let home = &quot;127.0.0.1&quot;.parse::&lt;IpAddr&gt;().unwrap();
</code></pre>
<p>We're creating an <code>IpAddr</code> instance by parsing a hardcoded string. We can see
that <code>&quot;127.0.0.1&quot;</code> is a valid IP address, so it's acceptable to use <code>unwrap</code>
here. However, having a hardcoded, valid string doesn't change the return type
of the <code>parse</code> method: we still get a <code>Result</code> value, and the compiler will
still make us handle the <code>Result</code> as if the <code>Err</code> variant is still a possibility
since the compiler isn't smart enough to see that this string is always a
valid IP address. If the IP address string came from a user instead of being
hardcoded into the program, and therefore <em>did</em> have a possibility of failure,
we'd definitely want to handle the <code>Result</code> in a more robust way instead.</p>
<h3>Guidelines for Error Handling</h3>
<p><code>panic!</code> when your code is in a situation where it's possible to be in a bad
state and:</p>
<p>It's advisable to have your code <code>panic!</code> when it's possible that you could end
up in a <em>bad state</em>---in this context, <em>bad state</em> is when some assumption,
guarantee, contract, or invariant has been broken, such as when invalid values,
contradictory values, or missing values are passed to your code---plus one or
more of the following:</p>
<ul>
<li>The bad state is not something that's <em>expected</em> to happen occasionally</li>
<li>Your code after this point needs to rely on not being in this bad state</li>
<li>There's not a good way to encode this information in the types you use</li>
</ul>
<p>By <em>bad state</em>, we mean some assumption, guarantee, contract, or invariant has
been broken. Some examples are invalid values, contradictory values, or
nothing when you expect to have something. If someone calls your code and
passes in values that don't make sense, the best thing might be to <code>panic!</code> and
alert the person using your library to the bug in their code so that they can
fix it during development. Similarly, <code>panic!</code> is often appropriate if you call
someone else's code that is out of your control, and it returns an invalid
state that you have no way of fixing.</p>
<p>Taking each point in turn:</p>
<p>Some bad states are expected to happen sometimes, and will happen no matter how
well you write your code. Examples of this include a parser being given
malformed data to parse, or an HTTP request returning a status that indicates
you have hit a rate limit. In these cases, you should indicate that failure is
an expected possibility by returning a <code>Result</code> and propagate these bad states
upwards so that the caller can decide how they would like to handle the
problem. <code>panic!</code> would not be the best way to handle these cases.</p>
<p>If someone calls your code and passes in values that don't make sense, the best
thing might be to <code>panic!</code> and alert the person using your library to the bug
in their code so that they can fix it during development. Similarly, <code>panic!</code>
is often appropriate if you're calling external code that is out of your
control, and it returns an invalid state that you have no way of fixing.</p>
<p>When a bad state is reached, but it's expected to happen no matter how well you
write your code, it's still more appropriate to return a <code>Result</code> rather than
calling <code>panic!</code>. Examples of this include a parser being given malformed data,
or an HTTP request returning a status that indicates you have hit a rate limit.
In these cases, you should indicate that failure is an expected possibility by
returning a <code>Result</code> in order to propagate these bad states upwards so that the
caller can decide how they would like to handle the problem. To <code>panic!</code>
wouldn't be the best way to handle these cases.</p>
<p>When your code performs operations on values, your code should verify the
values are valid first, then proceed confidently with the operations. This is
mostly for safety reasons: attempting to operate on invalid data can expose
your code to vulnerabilities. This is the main reason that the standard library
will <code>panic!</code> if you attempt an out-of-bounds array access: trying to access
memory that doesn't belong to the current data structure is a common security
problem. Functions often have <em>contracts</em>: their behavior is only guaranteed if
the inputs meet particular requirements. Panicking when the contract is
violated makes sense because a contract violation always indicates a
caller-side bug, and it is not a kind of error you want callers to have to
explicitly handle. In fact, there's no reasonable way for calling code to
recover: the calling <em>programmers</em> need to fix the code. Contracts for a
function, especially when a violation will cause a <code>panic</code>, should be explained
in the API documentation for the function.</p>
values are valid first, and <code>panic!</code> if the values aren't valid. This is mostly
for safety reasons: attempting to operate on invalid data can expose your code
to vulnerabilities. This is the main reason that the standard library will
<code>panic!</code> if you attempt an out-of-bounds array access: trying to access memory
that doesn't belong to the current data structure is a common security problem.
Functions often have <em>contracts</em>: their behavior is only guaranteed if the
inputs meet particular requirements. Panicking when the contract is violated
makes sense because a contract violation always indicates a caller-side bug,
and it is not a kind of error you want callers to have to explicitly handle. In
fact, there's no reasonable way for calling code to recover: the calling
<em>programmers</em> need to fix the code. Contracts for a function, especially when a
violation will cause a <code>panic</code>, should be explained in the API documentation
for the function.</p>
<p>Having lots of error checks in all of your functions would be verbose and
annoying, though. Luckily, our last guideline has a tip for this situation: use
Rust's type system (and thus the type checking the compiler does) to do a lot
of the checks for you. If your function takes a particular type as an argument,
you can proceed with your code's logic knowing that the compiler has already
ensured you have a valid value. For example, if you have a type rather than an
<code>Option</code>, you know that you will have something rather than nothing and you
don't have to have an explicit check to make sure. Another example is using an
unsigned integer type like <code>u32</code>, which ensures the argument value is never
negative.</p>
annoying, though. Luckily, you can use Rust's type system (and thus the type
checking the compiler does) to do a lot of the checks for you. If your function
takes a particular type as an argument, you can proceed with your code's logic
knowing that the compiler has already ensured you have a valid value. For
example, if you have a type rather than an <code>Option</code>, your program expects to
have <em>something</em> rather than <em>nothing</em>. Your code then doesn't have to handle
two cases for the <code>Some</code> and <code>None</code> variants, it will only have one case for
definitely having a value. Code trying to pass nothing to your function won't
even compile, so your function doesn't have to check for that case at runtime.
Another example is using an unsigned integer type like <code>u32</code>, which ensures the
argument value is never negative.</p>
<!-- Can you go into more detail explaining this last sentence? Why is a type
better to use than an Option?-->
<!-- I tried to reword, but I'm not sure if I made it any clearer. You don't
have to have extra checks, so your code is simpler; I'm not sure why it's not
clear that simpler is better. /Carol -->
<h3>Creating Custom Types for Validation</h3>
<p>Going a step further with the idea of using Rust's type system to ensure we
have a valid value, let's look at an example of creating a custom type for
validation. Recall the guessing game in Chapter 2, where our code asked the user
to guess a number between 1 and 100. We actually never validated that the
user's guess was between those numbers before checking it against our secret
number, only that it was positive. In this case, the consequences were not very
dire: our output of &quot;Too high&quot; or &quot;Too low&quot; would still be correct. It would be
a nice enhancement to guide the user towards valid guesses, though. We could
add a check after we parse the guess:</p>
<p>Let's take the idea of using Rust's type system to ensure we have a valid value
one step further, and look at creating a custom type for validation. Recall the
guessing game in Chapter 2, where our code asked the user to guess a number
between 1 and 100. We actually never validated that the user's guess was
between those numbers before checking it against our secret number, only that
it was positive. In this case, the consequences were not very dire: our output
of &quot;Too high&quot; or &quot;Too low&quot; would still be correct. It would be a useful
enhancement to guide the user towards valid guesses, though, and have different
behavior when a user guesses a number that's out of range versus when a user
types, for example, letters instead.</p>
<p>One way to do this would be to parse the guess as an <code>i32</code> instead of only a
<code>u32</code>, to allow potentially negative numbers, then add a check for the number
being in range:</p>
<pre><code class="language-rust,ignore">loop {
// snip
let guess: u32 = match guess.trim().parse() {
let guess: i32 = match guess.trim().parse() {
Ok(num) =&gt; num,
Err(_) =&gt; continue,
};
@@ -155,14 +218,16 @@ about the problem, and calls <code>continue</code> to start the next iteration o
and ask for another guess. After the <code>if</code> expression, we can proceed with the
comparisons between <code>guess</code> and the secret number knowing that guess is between
1 and 100.</p>
<p>If we had a situation where it was absolutely critical we had a value between 1
and 100, and we had many functions that had this requirement, it would be
tedious (and potentially impact performance) to have a check like this in every
function. Instead, we can make a new type and put the validations in one place,
in the type's constructor. Then our functions can use the type with the
confidence that we have values that meet our requirements. Listing 9-8 shows
one way to define a <code>Guess</code> type that will only create an instance of <code>Guess</code>
if the <code>new</code> function gets a value between 1 and 100:</p>
<p>However, this is not an ideal solution: if it was absolutely critical that the
program only operated on values between 1 and 100, and it had many functions
with this requirement, it would be tedious (and potentially impact performance)
to have a check like this in every function.</p>
<p>Instead, we can make a new type and put the validations in the type's
constructor rather than repeating them. That way, it's safe for functions to
use the new type in their signatures and confidently use the values they
receive. Listing 9-8 shows one way to define a <code>Guess</code> type that will only
create an instance of <code>Guess</code> if the <code>new</code> function receives a value between 1
and 100:</p>
<figure>
<pre><code class="language-rust">struct Guess {
value: u32,
@@ -185,35 +250,54 @@ impl Guess {
}
</code></pre>
<figcaption>
<p>Listing 9-8: A <code>Guess</code> type that will only hold values between 1 and 100</p>
<p>Listing 9-8: A <code>Guess</code> type that will only continue with values between 1 and
100</p>
</figcaption>
</figure>
<p>If code calling <code>Guess::new</code> passed in a value that was not between 1 and 100,
that would be a violation of the contract that <code>Guess::new</code> is relying on. This
function needs to signal to the calling code that it has a bug somewhere
leading to the contract violation. The conditions in which <code>Guess::new</code> might
panic should be discussed in its public-facing API documentation, which we will
cover in Chapter 14.</p>
<p>Important to note is the <code>value</code> field of the <code>Guess</code> struct is private, so
code using this struct may not set that value directly. Callers <em>must</em> use the
<code>Guess::new</code> constructor function to create an instance of <code>Guess</code>, and they
may read the value using the public <code>value</code> function, but they may not access
the field directly. This means any created instance of <code>Guess</code> that does not
cause a <code>panic!</code> when <code>new</code> is called is guaranteed to return numbers between 1
and 100 from its <code>value</code> function.</p>
<!-- I'll add wingding numbers in the libreoffice file /Carol -->
<p>First, we define a struct named <code>Guess</code> that has a field named <code>value</code> that
holds a <code>u32</code>. This is where the number will be stored.</p>
<p>Then we implement an associated function named <code>new</code> on <code>Guess</code> that is a
constructor of <code>Guess</code> values. The <code>new</code> function takes one argument named
<code>value</code> of type <code>u32</code> and returns a <code>Guess</code>. The code in the body of the <code>new</code>
function tests the <code>value</code> argument to make sure it is between 1 and 100. If
<code>value</code> doesn't pass this test, we call <code>panic!</code>, which will alert the
programmer who is calling this code that they have a bug they need to fix,
since creating a <code>Guess</code> with a <code>value</code> outside this range would violate the
contract that <code>Guess::new</code> is relying on. The conditions in which <code>Guess::new</code>
might panic should be discussed in its public-facing API documentation; we'll
cover documentation conventions around indicating the possibility of a <code>panic!</code>
in the API documentation that you create in Chapter 14. If <code>value</code> does pass
the test, we create a new <code>Guess</code> with its <code>value</code> field set to the <code>value</code>
argument, and return the <code>Guess</code>.</p>
<!-- I'm not sure if you mean the function that creates the guess type (so
listing 9-8) or the function that uses the guess type, below. You mean the
wider function needs a way to signal that there's a bug leading to contract
violation? -->
<!-- I'm not sure what part is confusing, and I'm not sure what you mean by
"wider function". I hope the slower explanation of the code has cleared
this up; please provide more detail on what's confusing if not. /Carol -->
<p>Next, we implement a method named <code>value</code> that borrows <code>self</code>, doesn't take any
other arguments, and returns a <code>u32</code>. This is a kind of method sometimes called
a <em>getter</em>, since its purpose is to get some data from its fields and return
it. This public method is necessary because the <code>value</code> field of the <code>Guess</code>
struct is private. It's important that the <code>value</code> field is private so that
code using the <code>Guess</code> struct is not allowed to set <code>value</code> directly: callers
<em>must</em> use the <code>Guess::new</code> constructor function to create an instance of
<code>Guess</code>, which ensures there's no way for a <code>Guess</code> to have a <code>value</code> that
hasn't been checked by the conditions in the constructor.</p>
<p>A function that takes as an argument or returns only numbers between 1 and 100
could then declare in its signature to take a <code>Guess</code> rather than a <code>u32</code>, and
would not need to do any additional checks in its body.</p>
could then declare in its signature that it takes a <code>Guess</code> rather than a
<code>u32</code>, and wouldn't need to do any additional checks in its body.</p>
<h2>Summary</h2>
<p>Rust's error handling features are designed to help you write more robust code.
The <code>panic!</code> macro signals that your program is in a state it can't handle, and
lets you tell the process to stop instead of trying to proceed with invalid or
incorrect values. The <code>Result</code> enum uses Rust's type system as a sign that
operations you call might fail in a way that your code could recover from. You
can use <code>Result</code> to tell code that calls yours that it needs to handle
potential success or failure as well. Using <code>panic!</code> and <code>Result</code> in the
appropriate situations will help your code be more reliable in the face of
inevitable problems.</p>
incorrect values. The <code>Result</code> enum uses Rust's type system to indicate that
operations might fail in a way that your code could recover from. You can use
<code>Result</code> to tell code that calls your code that it needs to handle potential
success or failure as well. Using <code>panic!</code> and <code>Result</code> in the appropriate
situations will make your code more reliable in the face of inevitable problems.</p>
<p>Now that we've seen useful ways that the standard library uses generics with
the <code>Option</code> and <code>Result</code> enums, let's talk about how generics work and how you
can make use of them in your code.</p>
+1 -1
View File
@@ -117,7 +117,7 @@ happen bc you forget to unlock, but drop unlocks automatically.</p>
<h2>Maybe make the I/O project concurrent?</h2>
<p>Might be a lot of boilerplate without scoped threads, maybe just allude.</p>
<p>This is a really rough sketch of some ideas that this chapter might cover.</p>
<p>From a comment of steveklabnik's on [the definitely not orange website]. &quot;that paper&quot; refers to <a href="http://www.hpl.hp.com/techreports/2004/HPL-2004-209.pdf">Boehm 2004</a>.</p>
<p>From a comment of steveklabnik's on <a href="https://news.ycombinator.com/item?id=13078384">the definitely not orange website</a>. &quot;that paper&quot; refers to <a href="http://www.hpl.hp.com/techreports/2004/HPL-2004-209.pdf">Boehm 2004</a>.</p>
<p>So for example, in that paper, 4.1 is about the problem of concurrent
modifiability. And indeed, it says</p>
<blockquote>
+577 -311
View File
File diff suppressed because it is too large Load Diff