4 ms·
you can do it that way if you wanted to let mut i = 0; for item in items { item.method(i); i += 1; } I don't know why you'd it that wa
by holmium 3y ago
you can do it that way if you wanted to
let mut i = 0;
for item in items {
item.method(i);
i += 1;
}
I don't know why you'd it that way, but Rust definitely isn't stopping you from doing that.
e: and as running example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- Pxtl 3y agoWell, the complaint in the article is that the idiomatic way is kind of inscrutable. I'm more than willing to let pragmatism win in those cases, since so many languages make this pattern a nuisance instead of providing a simple "enumerate this array with index" out-of-the-box as part of the standard library. IIRC you have to roll your own in Java, C#, and Powershell.
- lostmsu 3y agoIn C# there's a .Select overload, that gives your lambda both item and its index.