1 unstable release
0.1.0 | Oct 29, 2024 |
---|
#735 in Rust patterns
119 downloads per month
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 !