Get first and last from not cloneable iterator

So I have iterator it, that I get from iter_mut (and several conversation methods like .map, .skip_white and so on).

And I want to get modify first and last elements according to this iterator.
I wrote:

if let Some(first) = it.next() {    
  first.some_flag = true;
}
if let Some(last) = it.last() {
   last.other_flag = true;
}

it works, but not for case when sequence accessible from it has length 1.
In this case I need set to this element both flags to true.

How can I handle case when length == 1 and first == last?
If my iterator is not implement Clone trait?

Problem demonstration example:

#[derive(Default, Clone, Debug)]                                                                                                                                                
struct Foo { 3 implementations                                                                                                                                                  
    first: bool,                                                                                                                                                                
    last: bool,                                                                                                                                                                 
}                                                                                                                                                                               
                                                                                                                                                                                
fn main() {                                                                                                                                 
    let mut v = vec![Foo::default(); 1];                                                                                                                                        
    let mut it = v.iter_mut();                                                                                                                                                  
    if let Some(e) = it.next() {                                                                                                                                                
        e.first = true;                                                                                                                                                         
    }                                                                                                                                                                           
    if let Some(e) = it.last() {                                                                                                                                                
        e.last = true;                                                                                                                                                          
    }                                                                                                                                                                           
    println!("Results: {v:?}")                                                                                                                                                  
}                                                                                                                                                                               

You could nest that, something like:

    if let Some(e_first) = it.next() {                         
        e_first.first = true;

        let e_last = it.last().unwrap_or(e_first);
        e_last.last = true;
    }

If your iterator allows (double-ended), next_back() is a little more direct than last().

7 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.