Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branching a iterator without deep copying its elements

Iterators is consumed by map, so require cloned(equals to clone?) when mapping a iterator multiple times. I have following code:

fn main() {
    let a: Vec<(&str, (&i32, &i32))> = vec![("0", (&0, &1)), ("1", (&2, &3))];
    let iter = a.iter().map(|x| x.1 as (&i32, &i32));

    // occur clone process on i32?
    let xs: Vec<&i32> = iter.cloned().map(|(x, _)| x).collect();
    
    let ys: Vec<&i32> = iter.map(|(_, y)| y).collect();
    println!("{:?}", xs);
    println!("{:?}", ys);
}

The problem is that cloned method probably deep copy whole an iterated element even though the elements contain immutable references. Cloned elements is i32 in above, but it may actually have big structure. I want to explicitly shallow copy elements which contain references to i32 (actually bigger struct) when branch iterator iter. Is it possible?

like image 956
winjii Avatar asked Jul 24 '26 18:07

winjii


1 Answers

Use clone instead of cloned:

fn main() {
    let a: Vec<(&str, (&i32, &i32))> = vec![("0", (&0, &1)), ("1", (&2, &3))];
    let iter = a.iter().map(|x| x.1 as (&i32, &i32));

    // occur clone process on i32?
    let xs: Vec<&i32> = iter.clone().map(|(x, _)| x).collect();
    
    let ys: Vec<&i32> = iter.map(|(_, y)| y).collect();
    println!("{:?}", xs);
    println!("{:?}", ys);
}

Playground

clone clones the iterator itself. Meanwhile cloned clones the underlying elements, from the documentation:

Cloned: An iterator that clones the elements of an underlying iterator.

like image 173
Netwave Avatar answered Jul 28 '26 06:07

Netwave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!