@@ -83,7 +83,6 @@ fn main() {
83
83
let mut guess = String::new();
84
84
85
85
io::stdin().read_line(&mut guess)
86
- .ok()
87
86
.expect("Failed to read line");
88
87
89
88
println!("You guessed: {}", guess);
@@ -189,7 +188,6 @@ Let’s move forward:
189
188
190
189
``` rust,ignore
191
190
io::stdin().read_line(&mut guess)
192
- .ok()
193
191
.expect("Failed to read line");
194
192
```
195
193
@@ -245,7 +243,6 @@ a single line of text, it’s only the first part of the single logical line of
245
243
code:
246
244
247
245
``` rust,ignore
248
- .ok()
249
246
.expect("Failed to read line");
250
247
```
251
248
@@ -254,33 +251,27 @@ and other whitespace. This helps you split up long lines. We _could_ have
254
251
done:
255
252
256
253
``` rust,ignore
257
- io::stdin().read_line(&mut guess).ok(). expect("failed to read line");
254
+ io::stdin().read_line(&mut guess).expect("failed to read line");
258
255
```
259
256
260
- But that gets hard to read. So we’ve split it up, three lines for three
261
- method calls. We already talked about ` read_line() ` , but what about ` ok ()`
262
- and ` expect() ` ? Well, we already mentioned that ` read_line() ` puts what
263
- the user types into the ` &mut String` we pass it. But it also returns
264
- a value: in this case, an [ ` io::Result ` ] [ ioresult ] . Rust has a number of
265
- types named ` Result ` in its standard library: a generic [ ` Result ` ] [ result ] ,
266
- and then specific versions for sub-libraries, like ` io::Result ` .
257
+ But that gets hard to read. So we’ve split it up, three lines for three method
258
+ calls. We already talked about ` read_line() ` , but what about ` expect ()` ? Well,
259
+ we already mentioned that ` read_line() ` puts what the user types into the `&mut
260
+ String` we pass it. But it also returns a value: in this case, an
261
+ [ ` io::Result ` ] [ ioresult ] . Rust has a number of types named ` Result ` in its
262
+ standard library: a generic [ ` Result ` ] [ result ] , and then specific versions for
263
+ sub-libraries, like ` io::Result ` .
267
264
268
265
[ ioresult ] : ../std/io/type.Result.html
269
266
[ result ] : ../std/result/enum.Result.html
270
267
271
268
The purpose of these ` Result ` types is to encode error handling information.
272
269
Values of the ` Result ` type, like any type, have methods defined on them. In
273
- this case, ` io::Result ` has an ` ok() ` method, which says ‘we want to assume
274
- this value is a successful one. If not, just throw away the error
275
- information’. Why throw it away? Well, for a basic program, we just want to
276
- print a generic error, as basically any issue means we can’t continue. The
277
- [ ` ok() ` method] [ ok ] returns a value which has another method defined on it:
278
- ` expect() ` . The [ ` expect() ` method] [ expect ] takes a value it’s called on, and
279
- if it isn’t a successful one, [ ` panic! ` ] [ panic ] s with a message you
280
- passed it. A ` panic! ` like this will cause our program to crash, displaying
281
- the message.
282
-
283
- [ ok ] : ../std/result/enum.Result.html#method.ok
270
+ this case, ` io::Result ` has an [ ` expect() ` method] [ expect ] that takes a value
271
+ it’s called on, and if it isn’t a successful one, [ ` panic! ` ] [ panic ] s with a
272
+ message you passed it. A ` panic! ` like this will cause our program to crash,
273
+ displaying the message.
274
+
284
275
[ expect ] : ../std/option/enum.Option.html#method.expect
285
276
[ panic ] : error-handling.html
286
277
@@ -468,7 +459,6 @@ fn main() {
468
459
let mut guess = String::new();
469
460
470
461
io::stdin().read_line(&mut guess)
471
- .ok()
472
462
.expect("failed to read line");
473
463
474
464
println!("You guessed: {}", guess);
@@ -557,7 +547,6 @@ fn main() {
557
547
let mut guess = String::new();
558
548
559
549
io::stdin().read_line(&mut guess)
560
- .ok()
561
550
.expect("failed to read line");
562
551
563
552
println!("You guessed: {}", guess);
@@ -668,11 +657,9 @@ fn main() {
668
657
let mut guess = String::new ();
669
658
670
659
io::stdin ().read_line(& mut guess)
671
- .ok ()
672
660
.expect(" failed to read line" );
673
661
674
662
let guess: u32 = guess.trim().parse ()
675
- .ok ()
676
663
.expect(" Please type a number!" );
677
664
678
665
println! (" You guessed: {}" , guess);
@@ -689,7 +676,6 @@ The new three lines:
689
676
690
677
` ` ` rust,ignore
691
678
let guess: u32 = guess.trim().parse ()
692
- .ok ()
693
679
.expect(" Please type a number!" );
694
680
` ` `
695
681
@@ -706,27 +692,26 @@ We bind `guess` to an expression that looks like something we wrote earlier:
706
692
guess.trim().parse ()
707
693
` ` `
708
694
709
- Followed by an ` ok().expect()` invocation. Here, ` guess` refers to the old
710
- ` guess` , the one that was a ` String` with our input in it. The ` trim()`
711
- method on ` String` s will eliminate any white space at the beginning and end of
712
- our string. This is important, as we had to press the ‘return’ key to satisfy
713
- ` read_line()` . This means that if we type ` 5` and hit return, ` guess` looks
714
- like this: ` 5\n ` . The ` \n ` represents ‘newline’, the enter key. `trim ()` gets
715
- rid of this, leaving our string with just the ` 5` . The [` parse()` method on
716
- strings][parse] parses a string into some kind of number. Since it can parse a
717
- variety of numbers, we need to give Rust a hint as to the exact type of number
718
- we want. Hence, ` let guess: u32` . The colon (` :` ) after ` guess` tells Rust
719
- we’re going to annotate its type. ` u32` is an unsigned, thirty-two bit
720
- integer. Rust has [a number of built-in number types][number], but we’ve
721
- chosen ` u32` . It’s a good default choice for a small positive number.
695
+ Here, ` guess` refers to the old ` guess` , the one that was a ` String` with our
696
+ input in it. The ` trim()` method on ` String` s will eliminate any white space at
697
+ the beginning and end of our string. This is important, as we had to press the
698
+ ‘return’ key to satisfy ` read_line()` . This means that if we type ` 5` and hit
699
+ return, ` guess` looks like this: ` 5\n ` . The ` \n ` represents ‘newline’, the
700
+ enter key. `trim ()` gets rid of this, leaving our string with just the ` 5` . The
701
+ [` parse()` method on strings][parse] parses a string into some kind of number.
702
+ Since it can parse a variety of numbers, we need to give Rust a hint as to the
703
+ exact type of number we want. Hence, ` let guess: u32` . The colon (` :` ) after
704
+ ` guess` tells Rust we’re going to annotate its type. ` u32` is an unsigned,
705
+ thirty-two bit integer. Rust has [a number of built-in number types][number],
706
+ but we’ve chosen ` u32` . It’s a good default choice for a small positive number.
722
707
723
708
[parse]: ../std/primitive.str.html#method.parse
724
709
[number]: primitive-types.html#numeric-types
725
710
726
711
Just like ` read_line()` , our call to ` parse()` could cause an error. What if
727
712
our string contained ` A👍%` ? There’d be no way to convert that to a number. As
728
- such, we’ll do the same thing we did with `read_line ()` : use the ` ok ()` and
729
- ` expect() ` methods to crash if there’s an error.
713
+ such, we’ll do the same thing we did with `read_line ()` : use the ` expect ()`
714
+ method to crash if there’s an error.
730
715
731
716
Let’s try our program out!
732
717
@@ -773,11 +758,9 @@ fn main() {
773
758
let mut guess = String::new ();
774
759
775
760
io::stdin ().read_line(& mut guess)
776
- .ok ()
777
761
.expect(" failed to read line" );
778
762
779
763
let guess: u32 = guess.trim().parse ()
780
- .ok ()
781
764
.expect(" Please type a number!" );
782
765
783
766
println! (" You guessed: {}" , guess);
@@ -841,11 +824,9 @@ fn main() {
841
824
let mut guess = String::new ();
842
825
843
826
io::stdin ().read_line(& mut guess)
844
- .ok ()
845
827
.expect(" failed to read line" );
846
828
847
829
let guess: u32 = guess.trim().parse ()
848
- .ok ()
849
830
.expect(" Please type a number!" );
850
831
851
832
println! (" You guessed: {}" , guess);
@@ -888,7 +869,6 @@ fn main() {
888
869
let mut guess = String::new ();
889
870
890
871
io::stdin ().read_line(& mut guess)
891
- .ok ()
892
872
.expect(" failed to read line" );
893
873
894
874
let guess: u32 = match guess.trim().parse () {
@@ -920,7 +900,6 @@ let guess: u32 = match guess.trim().parse() {
920
900
` ` `
921
901
922
902
This is how you generally move from ‘crash on error’ to ‘actually handle the
923
- error’, by switching from ` ok().expect()` to a ` match` statement. The ` Result`
924
903
returned by ` parse()` is an ` enum` just like ` Ordering` , but in this case, each
925
904
variant has some data associated with it: ` Ok` is a success, and ` Err` is a
926
905
failure. Each contains more information: the successfully parsed integer, or an
@@ -977,7 +956,6 @@ fn main() {
977
956
let mut guess = String::new ();
978
957
979
958
io::stdin ().read_line(& mut guess)
980
- .ok ()
981
959
.expect(" failed to read line" );
982
960
983
961
let guess: u32 = match guess.trim().parse () {
0 commit comments