|
|
|
|
|
by Twisol
2612 days ago
|
|
> C++ lets you have as many iterators as you like into the same container While it's true that Rust doesn't let you have multiple mutable iterators, it's worth pointing out that you can certainly have multiple immutable iterators. let items = &[42, 101];
// All products (a*a, a*b, b*a, b*b)
for x in items {
for y in items {
println!("{}", x * y);
}
}
println!("");
// Pairwise products (a*a, b*b)
let pairs = Iterator::zip(items.iter(), items.iter());
let pointwise_products = pairs.map(|(x, y)| x * y);
for product in pointwise_products {
println!("{}", product);
}
|
|
Even if i, j, k are const iterators, it's not pleasant to translate this to Rust. So it's not an issue of whether Rust lets you have multiple iterators or not, the issue is that Rust iterators are strictly less expressive than C++ iterators.
And that's okay. It's a tradeoff.
For more information about how iterators are used in C++, I would refer to the <algorithms> portion of the standard library. https://en.cppreference.com/w/cpp/algorithm