Skip to content

Instantly share code, notes, and snippets.

@s3rvac
Created April 17, 2017 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s3rvac/1970120d5512f6de547054370fee5e10 to your computer and use it in GitHub Desktop.
Save s3rvac/1970120d5512f6de547054370fee5e10 to your computer and use it in GitHub Desktop.
Converting a Vec<Item> to Vec<Option<Item>> in Rust
// 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