Last active
November 18, 2016 15:13
-
-
Save chespinoza/f002dacc9fdb7cc8278f12b6d7bcacef to your computer and use it in GitHub Desktop.
Rust Vectors.
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
fn main() { | |
let v = vec![1, 2, 3]; | |
let mut iter = v.into_iter(); | |
let n = iter.next(); | |
println!("{:?}", n.unwrap()); | |
let n = iter.next(); | |
println!("{:?}", n.unwrap()); | |
let n = iter.next(); | |
println!("{:?}", n.unwrap()); | |
// This return a Panic because the isn't a Value but "None" | |
// let n = iter.next(); | |
// println!("{:?}", n.unwrap()); | |
// | |
// This could be a solution, but is not enough | |
let n = iter.next(); | |
println!("{:?}", n.unwrap_or(0)); | |
// A better approach could be match the result | |
// and then perform custom actions. | |
let n = iter.next(); | |
print_vec_val(n); | |
} | |
// This could be a better approach to avoid unwrap panics | |
fn print_vec_val(val: Option<i32>) { | |
match val { | |
Some(value) => println!("{:?}", value), | |
None => println!("No Values!"), | |
} | |
} | |
//The best solution IMO is not use a next() function to iterate vectors :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment