As Sebastian Redl points out, checking the len
gth of each chunk is the better solution for your specific case.
To answer the question you asked ("Use all but the last element from an iterator"), you can use Iterator::peekable
to look ahead one. That will tell you if you are on the last item or not and you can decide to skip processing it if so.
let things = [0, 1, 2, 3, 4];
let mut chunks = things.chunks(2).peekable();
while let Some(chunk) = chunks.next() {
if chunks.peek().is_some() {
print!("Not the last: ");
} else {
print!("This is the last: ")
}
println!("{:?}", chunk);
}
To be sure that all parts have equal length, I just want to drop that last element
Always dropping the last element won't do this. For example, if you evenly chunk up your input, then always dropping the last element would lose a full chunk. You'd have to do some pre-calculation to decide if you need to drop it or not.