Created
April 17, 2017 10:01
-
-
Save s3rvac/1970120d5512f6de547054370fee5e10 to your computer and use it in GitHub Desktop.
Converting a Vec<Item> to Vec<Option<Item>> in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We need to use Vec::into_iter() instead of Vec::iter() to create a consuming | |
// iterator that moves each value out of the vector. | |
fn main() { | |
let v = vec![1, 2, 3]; | |
let v: Vec<Option<i32>> = v.into_iter().map(Some).collect(); | |
assert_eq!(v, [Some(1), Some(2), Some(3)]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment