Created
April 14, 2020 17:45
-
-
Save cppio/d93cc6fc5c7b459b2c8b7c2e2b7db88c to your computer and use it in GitHub Desktop.
Simple snippet showing how to skip the last item in an iterator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let x = [0, 1, 2]; | |
| let y: Vec<_> = x | |
| .iter() | |
| .scan(None, |prev, i| Some(prev.replace(i))) | |
| .flatten() | |
| .collect(); | |
| println!("x = {:?}", x); // x = [0, 1, 2] | |
| println!("y = {:?}", y); // y = [0, 1] | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works by transforming
using
scan()intoand then calling
flatten()to get