@@ -9,6 +9,8 @@ in the surrounding scope like a normal `let`, or else diverge (e.g. `break`,
9
9
` return ` , ` panic! ` ) when the pattern doesn't match.
10
10
11
11
``` rust
12
+ use std :: str :: FromStr ;
13
+
12
14
fn get_count_item (s : & str ) -> (u64 , & str ) {
13
15
let mut it = s . split (' ' );
14
16
let (Some (count_str ), Some (item )) = (it . next (), it . next ()) else {
@@ -19,6 +21,7 @@ fn get_count_item(s: &str) -> (u64, &str) {
19
21
};
20
22
(count , item )
21
23
}
24
+
22
25
assert_eq! (get_count_item (" 3 chairs" ), (3 , " chairs" ));
23
26
```
24
27
@@ -27,6 +30,10 @@ The scope of name bindings is the main thing that makes this different from
27
30
patterns with an unfortunate bit of repetition and an outer ` let ` :
28
31
29
32
``` rust
33
+ # use std :: str :: FromStr ;
34
+ #
35
+ # fn get_count_item (s : & str ) -> (u64 , & str ) {
36
+ # let mut it = s . split (' ' );
30
37
let (count_str , item ) = match (it . next (), it . next ()) {
31
38
(Some (count_str ), Some (item )) => (count_str , item ),
32
39
_ => panic! (" Can't segment count item pair: '{s}'" ),
@@ -36,6 +43,10 @@ patterns with an unfortunate bit of repetition and an outer `let`:
36
43
} else {
37
44
panic! (" Can't parse integer: '{count_str}'" );
38
45
};
46
+ # (count , item )
47
+ # }
48
+ #
49
+ # assert_eq! (get_count_item (" 3 chairs" ), (3 , " chairs" ));
39
50
```
40
51
41
52
### See also:
0 commit comments