Skip to content

Instantly share code, notes, and snippets.

@FGRibreau

FGRibreau/iterator_tail.rs

Last active Jan 27, 2017
Embed
What would you like to do?
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