#iterator #peekable #peek #is-last #is-not-last

iter_peek_end

Is the current element the last one of a peekable iterator ? (is_last() and is_not_last())

1 unstable release

0.1.0 Oct 29, 2024

#740 in Rust patterns

Download history 61/week @ 2024-10-23 59/week @ 2024-10-30 6/week @ 2024-11-06

126 downloads per month

MIT/Apache

7KB

Define the IterPeekEnd trait that work on peekable iterator, to know if the current element is the last one of the iterator.

pub trait IterPeekEnd
{ 
    fn is_last(&mut self) -> bool;
    fn is_not_last(&mut self) -> bool { !self.is_last() }
}

Useful to use when iterating for doing some processing between each value of an iterator :

let my_vec = vec![1, 2, 3];
let mut it = my_vec.iter().peekable();
        
while let Some(v) = it.next()
{
    print!("{}", v);
    if it.is_not_last() 
    {
        print!(", ");
    }
}

will display => 1, 2, 3

Notice the lack of comma after the last element.

Based on @ctrl-alt-delor anwser on StackOverflow : How to check if for loop is on the last element of an iterator?, thank you !


lib.rs:

Define the [IterPeekEnd] trait that work on peekable iterator, to know if the current element is the last one of the iterator.

pub trait IterPeekEnd
{ 
    fn is_last(&mut self) -> bool;
    fn is_not_last(&mut self) -> bool { !self.is_last() }
}

Useful to use when iterating for doing some processing between each value of an iterator :

use iter_peek_end::*;

let my_vec = vec![1, 2, 3];
let mut it = my_vec.iter().peekable();
        
while let Some(v) = it.next()
{
    print!("{}", v);
    if it.is_not_last() 
    {
        print!(", ");
    }
}

will display => 1, 2, 3

Notice the lack of comma after the three / the last element.

Based on @ctrl-alt-delor anwser on StackOverflow : How to check if for loop is on the last element of an iterator?, thank you !

No runtime deps