4 ms·
Sure, you can do something like that, but it is unnecessary. Like Python, you can use "enumerate" to give you a tuple that that will give you `i` automatically.
by nu11ptr 4y ago
Sure, you can do something like that, but it is unnecessary. Like Python, you can use "enumerate" to give you a tuple that that will give you `i` automatically.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=59c845d8ba631cf0e0e84df3c34d20f2 https://play.rust-lang.org/?version=stable&mode=debug&editio...
struct Item(u32);
impl Item {
fn do_thing(&self, i: usize) {
println!("The value of `i` is: {i}");
}
}
fn main() {
let items = vec![Item(1), Item(2), Item(3)];
for (i, item) in items.iter().enumerate() {
item.do_thing(i);
}
}