Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Rust equivalent of Rx's `startWith`?

Tags:

iterator

rust

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

like image 892
I60R Avatar asked Jan 31 '18 20:01

I60R


1 Answers

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);
    }
}
like image 131
Shepmaster Avatar answered Nov 07 '22 02:11

Shepmaster