'How to deal with errors gracefully in map method of Option?
In the Rust By Example book show us a way to handle errors in the map method of Iterator:
let strings = vec!["93", "tofu", "18"];
let numbers: Result<Vec<_>, _> = strings.into_iter().map(|s| s.parse::<i32>()).collect();
println!("Results: {:?}", numbers);
Is it a similar way to deal with errors in Option like the following?
let a: Option<&str> = Some("tofu");
let b: Result<Option<i32>, _> = a.map(|a| a.parse::<i32>());
println!("Results: {:?}", b);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
