What is the equivalent of Rust Rx `startWith`?

Is there any predefined function to start iteration with a custom element using Rust iterators?

+4
source share
1 answer

iteration using custom element

If you have one item, use iter::once.

If you have multiple items, use iter::repeatin conjunction with Iterator::take.

to start iteration with

Use Iterator::chain.

Combine 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);
    }
}
+5
source

Source: https://habr.com/ru/post/1693000/


All Articles