Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Last active January 27, 2017 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FGRibreau/9bab6501c13367e787b5f31dc1d670f4 to your computer and use it in GitHub Desktop.
Save FGRibreau/9bab6501c13367e787b5f31dc1d670f4 to your computer and use it in GitHub Desktop.
tail() for Rust iterators
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