Skip to content

Instantly share code, notes, and snippets.

@kriogenia
Created June 3, 2022 12:49
Show Gist options
  • Save kriogenia/c1ba23c3846a758af053878cf771d811 to your computer and use it in GitHub Desktop.
Save kriogenia/c1ba23c3846a758af053878cf771d811 to your computer and use it in GitHub Desktop.
enumerate
fn main() {
let vec = vec!["f", "o", "o"];
// instead of this
for i in 0..vec.len() {
println!("The character at {i} is {}", vec[i]);
}
// you can use this
for (i, char) in vec.iter().enumerate() {
println!("The character at {i} is {char}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment