Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain two iterators while lazily constructing the second one

Tags:

rust

I'd like a method like Iterator::chain() that only computes the argument iterator when it's needed. In the following code, expensive_function should never be called:

use std::{thread, time};

fn expensive_function() -> Vec<u64> {
    thread::sleep(time::Duration::from_secs(5));
    vec![4, 5, 6]
}

pub fn main() {
    let nums = [1, 2, 3];
    for &i in nums.iter().chain(expensive_function().iter()) {
        if i > 2 {
            break;
        } else {
            println!("{}", i);
        }
    }
}
like image 458
Morten Lohne Avatar asked Dec 13 '22 17:12

Morten Lohne


1 Answers

One possible approach: delegate the expensive computation to an iterator adaptor.

let nums = [1, 2, 3];
for i in nums.iter()
    .cloned()
    .chain([()].into_iter().flat_map(|_| expensive_function()))
{
    if i > 2 {
        break;
    } else {
        println!("{}", i);
    }
}

Playground

The passed iterator is the result of flat-mapping a dummy unit value () to the list of values, which is lazy. Since the iterator needs to own the respective outcome of that computation, I chose to copy the number from the array.

like image 109
E_net4 stands with Ukraine Avatar answered Dec 19 '22 12:12

E_net4 stands with Ukraine