Created
June 3, 2022 12:49
-
-
Save kriogenia/c1ba23c3846a758af053878cf771d811 to your computer and use it in GitHub Desktop.
enumerate
This file contains 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 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