Skip to content

Commit 28eba7b

Browse files
Mention the ? operator in Rust
1 parent 3aa53e0 commit 28eba7b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/language/nullability-and-optionality.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ let person1: Option<Person> = None;
8585
println!("{:?}", person1.and_then(|p| p.name.map(|name| name.first_name))); // None
8686
```
8787

88+
The `?` operator (mentioned in the previous chapter), can also be used to handle `Option`s.
89+
It returns from the function with `None` if a `None` is encountered, or else continues with the
90+
`Some` value:
91+
```rust
92+
fn foo(optional: Option<i32>) -> Option<String> {
93+
let value = optional?;
94+
Some(value.to_string())
95+
}
96+
```
97+
8898
## Null-coalescing operator
8999

90100
The null-coalescing operator (`??`) is typically used to default to another

0 commit comments

Comments
 (0)