Last active
January 27, 2017 17:41
-
-
Save FGRibreau/9bab6501c13367e787b5f31dc1d670f4 to your computer and use it in GitHub Desktop.
tail() for Rust iterators
This file contains 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
use std::iter::FromIterator; | |
trait ExtendedIterator: Iterator { | |
fn tail<B>(&mut self) -> B where B:FromIterator<Self::Item>, Self::Item: Eq + Clone, Self: Sized{ | |
self.skip(1).collect::<B>() | |
} | |
} | |
impl<I> ExtendedIterator for I where I: Iterator {} | |
fn main() { | |
let arr = vec!["okok@a.a.com", "a", "b"]; | |
let t: Vec<_> = arr.iter().tail(); | |
println!("Tail: {:?}", t); | |
// But more interestingly, instead of: | |
let split = "okok@oko@k.com".split("@"); | |
let vec:Vec<&str> = split.skip(1).collect(); | |
println!("Before: {:?}", vec); | |
// you can now do: | |
let tail: Vec<&str> = "okok@oko@k.com".split("@").tail(); | |
// because Split implements Iterator !! | |
println!("After: {:?}", tail); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment