Skip to content

Commit e91aa71

Browse files
authored
Merge pull request #215 from Manishearth/fx
Better example
2 parents 99afa61 + 47f8108 commit e91aa71

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

_posts/2017-11-22-Rust-1.22.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ In Rust 1.22, basic usage of `?` with `Option<T>` is now stable.
4242
This code will now compile:
4343

4444
```rust
45-
fn try_option_some() -> Option<u8> {
46-
let val = Some(1)?;
47-
Some(val)
45+
fn add_one_to_first(list: &[u8]) -> Option<u8> {
46+
// get first element, if exists
47+
// return None if it doesn't
48+
let first = list.get(0)?;
49+
Some(first + 1)
4850
}
49-
assert_eq!(try_option_some(), Some(1));
5051

51-
fn try_option_none() -> Option<u8> {
52-
let val = None?;
53-
Some(val)
54-
}
55-
assert_eq!(try_option_none(), None);
52+
assert_eq!(add_one_to_first(&[10, 20, 30]), Some(11));
53+
assert_eq!(add_one_to_first(&[]), None);
5654
```
5755

5856
However, this functionality is still a bit limited; you cannot yet write

0 commit comments

Comments
 (0)