Rust now allows the ?
operator to be used on Option
. The try_opt!
example further below
can be rewritten as the following:
```Rust use std::collections::HashMap;
fn mapaddchecked(map: &HashMap<&str, i32>, lhs: &str, rhs: &str) -> Option
fn main() { let mut map = HashMap::new(); map.insert("foo", 2); map.insert("bar", 4); map.insert("baz", 12); asserteq!(mapaddchecked(&map, "foo", "bar"), Some(6)); asserteq!(mapaddchecked(&map, "baz", "qux"), None); } ```
Helper macro for unwrapping Option
values while returning early with an
error if the value of the expression is None
. Can only be used in
functions that return Option
because of the early return of None
that
it provides.
```Rust
extern crate try_opt;
use std::collections::HashMap;
fn mapaddchecked(map: &HashMap<&str, i32>, lhs: &str, rhs: &str) -> Option
fn main() { let mut map = HashMap::new(); map.insert("foo", 2); map.insert("bar", 4); map.insert("baz", 12); asserteq!(mapaddchecked(&map, "foo", "bar"), Some(6)); asserteq!(mapaddchecked(&map, "baz", "qux"), None); } ```