Crates.io | iter_peek_end |
lib.rs | iter_peek_end |
version | 0.1.0 |
source | src |
created_at | 2024-10-29 14:17:37.204118 |
updated_at | 2024-10-29 14:17:37.204118 |
description | Is the current element the last one of a peekable iterator ? (`is_last()` and `is_not_last()`) |
homepage | |
repository | https://github.com/Thomas-Mewily/iter_peek_end |
max_upload_size | |
id | 1427067 |
size | 6,567 |
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 !