Is there any predefined function to start iteration with a custom element using Rust's iterators?
iteration with a custom element
If you have a single element, use iter::once
.
If you have multiple elements, use iter::repeat
coupled with Iterator::take
.
to start iteration with
Use Iterator::chain
.
Put together:
use std::iter;
fn main() {
let some_iterator = 1..10;
let start_with = iter::repeat(42).take(5);
let together = start_with.chain(some_iterator);
for i in together {
println!("{}", i);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With