-
Notifications
You must be signed in to change notification settings - Fork 0
/
let_else.rs
43 lines (38 loc) · 834 Bytes
/
let_else.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::str::FromStr;
#[test] fn ex_1() {
let res = Some(42);
// let else - since rust 1.66
// before:
let x = match res {
Some(x) => x,
None => return,
};
// now:
let Some(x) = res else { return };
assert_eq!(x, 42);
}
// ex-2
fn process_str(s: &str) -> u64 {
// Using it like an "early return"
// u64::from_str(s) -> Result<Self, ParseIntError>
let Ok(count) = u64::from_str(s) else {
panic!("Can't parse integer: '{s}'");
};
// instead of
let res = u64::from_str(s);
if res.is_err() {
return 42
}
let count = res.unwrap();
// . . . do_something_with(count)
count + 1
}
#[test]
fn ex2_simple_works() {
assert_eq!(process_str("3"), 4);
}
#[test]
#[should_panic]
fn ex3_panics() {
assert_eq!(process_str("aa"), 3);
}