@@ -117,8 +117,8 @@ the first example. This is because the
117
117
panic is embedded in the calls to ` unwrap ` .
118
118
119
119
To “unwrap” something in Rust is to say, “Give me the result of the
120
- computation, and if there was an error, just panic and stop the program.”
121
- It would be better if we just showed the code for unwrapping because it is so
120
+ computation, and if there was an error, panic and stop the program.”
121
+ It would be better if we showed the code for unwrapping because it is so
122
122
simple, but to do that, we will first need to explore the ` Option ` and ` Result `
123
123
types. Both of these types have a method called ` unwrap ` defined on them.
124
124
@@ -154,7 +154,7 @@ fn find(haystack: &str, needle: char) -> Option<usize> {
154
154
}
155
155
```
156
156
157
- Notice that when this function finds a matching character, it doesn't just
157
+ Notice that when this function finds a matching character, it doesn't only
158
158
return the ` offset ` . Instead, it returns ` Some(offset) ` . ` Some ` is a variant or
159
159
a * value constructor* for the ` Option ` type. You can think of it as a function
160
160
with the type ` fn<T>(value: T) -> Option<T> ` . Correspondingly, ` None ` is also a
@@ -216,7 +216,7 @@ we saw how to use `find` to discover the extension in a file name. Of course,
216
216
not all file names have a ` . ` in them, so it's possible that the file name has
217
217
no extension. This * possibility of absence* is encoded into the types using
218
218
` Option<T> ` . In other words, the compiler will force us to address the
219
- possibility that an extension does not exist. In our case, we just print out a
219
+ possibility that an extension does not exist. In our case, we only print out a
220
220
message saying as such.
221
221
222
222
Getting the extension of a file name is a pretty common operation, so it makes
@@ -248,7 +248,7 @@ tiresome.
248
248
249
249
In fact, the case analysis in ` extension_explicit ` follows a very common
250
250
pattern: * map* a function on to the value inside of an ` Option<T> ` , unless the
251
- option is ` None ` , in which case, just return ` None ` .
251
+ option is ` None ` , in which case, return ` None ` .
252
252
253
253
Rust has parametric polymorphism, so it is very easy to define a combinator
254
254
that abstracts this pattern:
@@ -350,7 +350,7 @@ fn file_name(file_path: &str) -> Option<&str> {
350
350
}
351
351
```
352
352
353
- You might think that we could just use the ` map ` combinator to reduce the case
353
+ You might think that we could use the ` map ` combinator to reduce the case
354
354
analysis, but its type doesn't quite fit. Namely, ` map ` takes a function that
355
355
does something only with the inner value. The result of that function is then
356
356
* always* [ rewrapped with ` Some ` ] ( #code-option-map ) . Instead, we need something
@@ -670,7 +670,7 @@ The tricky aspect here is that `argv.nth(1)` produces an `Option` while
670
670
with both an ` Option ` and a ` Result ` , the solution is * usually* to convert the
671
671
` Option ` to a ` Result ` . In our case, the absence of a command line parameter
672
672
(from ` env::args() ` ) means the user didn't invoke the program correctly. We
673
- could just use a ` String ` to describe the error. Let's try:
673
+ could use a ` String ` to describe the error. Let's try:
674
674
675
675
<span id =" code-error-double-string " ></span >
676
676
@@ -709,7 +709,7 @@ fn ok_or<T, E>(option: Option<T>, err: E) -> Result<T, E> {
709
709
710
710
The other new combinator used here is
711
711
[ ` Result::map_err ` ] ( ../std/result/enum.Result.html#method.map_err ) .
712
- This is just like ` Result::map ` , except it maps a function on to the * error*
712
+ This is like ` Result::map ` , except it maps a function on to the * error*
713
713
portion of a ` Result ` value. If the ` Result ` is an ` Ok(...) ` value, then it is
714
714
returned unmodified.
715
715
@@ -841,7 +841,7 @@ example, the very last call to `map` multiplies the `Ok(...)` value (which is
841
841
an ` i32 ` ) by ` 2 ` . If an error had occurred before that point, this operation
842
842
would have been skipped because of how ` map ` is defined.
843
843
844
- ` map_err ` is the trick that makes all of this work. ` map_err ` is just like
844
+ ` map_err ` is the trick that makes all of this work. ` map_err ` is like
845
845
` map ` , except it applies a function to the ` Err(...) ` value of a ` Result ` . In
846
846
this case, we want to convert all of our errors to one type: ` String ` . Since
847
847
both ` io::Error ` and ` num::ParseIntError ` implement ` ToString ` , we can call the
@@ -901,7 +901,7 @@ reduce explicit case analysis. Combinators aren't the only way.
901
901
## The ` try! ` macro
902
902
903
903
A cornerstone of error handling in Rust is the ` try! ` macro. The ` try! ` macro
904
- abstracts case analysis just like combinators, but unlike combinators, it also
904
+ abstracts case analysis like combinators, but unlike combinators, it also
905
905
abstracts * control flow* . Namely, it can abstract the * early return* pattern
906
906
seen above.
907
907
@@ -1461,7 +1461,7 @@ expose its representation (like
1461
1461
[ ` ErrorKind ` ] ( ../std/io/enum.ErrorKind.html ) ) or keep it hidden (like
1462
1462
[ ` ParseIntError ` ] ( ../std/num/struct.ParseIntError.html ) ). Regardless
1463
1463
of how you do it, it's usually good practice to at least provide some
1464
- information about the error beyond just its ` String `
1464
+ information about the error beyond its ` String `
1465
1465
representation. But certainly, this will vary depending on use cases.
1466
1466
1467
1467
At a minimum, you should probably implement the
@@ -1499,7 +1499,7 @@ that can go wrong!
1499
1499
The data we'll be using comes from the [ Data Science
1500
1500
Toolkit] [ 11 ] . I've prepared some data from it for this exercise. You
1501
1501
can either grab the [ world population data] [ 12 ] (41MB gzip compressed,
1502
- 145MB uncompressed) or just the [ US population data] [ 13 ] (2.2MB gzip
1502
+ 145MB uncompressed) or only the [ US population data] [ 13 ] (2.2MB gzip
1503
1503
compressed, 7.2MB uncompressed).
1504
1504
1505
1505
Up until now, we've kept the code limited to Rust's standard library. For a real
@@ -1706,7 +1706,7 @@ compiler can no longer reason about its underlying type.
1706
1706
1707
1707
[ Previously] ( #the-limits-of-combinators ) we started refactoring our code by
1708
1708
changing the type of our function from ` T ` to ` Result<T, OurErrorType> ` . In
1709
- this case, ` OurErrorType ` is just ` Box<Error> ` . But what's ` T ` ? And can we add
1709
+ this case, ` OurErrorType ` is only ` Box<Error> ` . But what's ` T ` ? And can we add
1710
1710
a return type to ` main ` ?
1711
1711
1712
1712
The answer to the second question is no, we can't. That means we'll need to
@@ -1924,7 +1924,7 @@ parser out of
1924
1924
But how can we use the same code over both types? There's actually a
1925
1925
couple ways we could go about this. One way is to write ` search ` such
1926
1926
that it is generic on some type parameter ` R ` that satisfies
1927
- ` io::Read ` . Another way is to just use trait objects:
1927
+ ` io::Read ` . Another way is to use trait objects:
1928
1928
1929
1929
``` rust,ignore
1930
1930
fn search<P: AsRef<Path>>
@@ -2081,7 +2081,7 @@ opts.optflag("q", "quiet", "Silences errors and warnings.");
2081
2081
...
2082
2082
```
2083
2083
2084
- Now we just need to implement our “quiet” functionality. This requires us to
2084
+ Now we only need to implement our “quiet” functionality. This requires us to
2085
2085
tweak the case analysis in ` main ` :
2086
2086
2087
2087
``` rust,ignore
@@ -2114,7 +2114,7 @@ handling in Rust. These are some good “rules of thumb." They are emphatically
2114
2114
heuristics!
2115
2115
2116
2116
* If you're writing short example code that would be overburdened by error
2117
- handling, it's probably just fine to use ` unwrap ` (whether that's
2117
+ handling, it's probably fine to use ` unwrap ` (whether that's
2118
2118
[ ` Result::unwrap ` ] ( ../std/result/enum.Result.html#method.unwrap ) ,
2119
2119
[ ` Option::unwrap ` ] ( ../std/option/enum.Option.html#method.unwrap )
2120
2120
or preferably
0 commit comments