iter_peek_end

Crates.ioiter_peek_end
lib.rsiter_peek_end
version0.1.0
sourcesrc
created_at2024-10-29 14:17:37.204118
updated_at2024-10-29 14:17:37.204118
descriptionIs the current element the last one of a peekable iterator ? (`is_last()` and `is_not_last()`)
homepage
repositoryhttps://github.com/Thomas-Mewily/iter_peek_end
max_upload_size
id1427067
size6,567
Thomas Mewily (Thomas-Mewily)

documentation

README

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 !

Commit count: 2

cargo fmt