Skip to content

Instantly share code, notes, and snippets.



Last active Jan 27, 2017
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{
impl<I> ExtendedIterator for I where I: Iterator {}
fn main() {
let arr = vec!["", "a", "b"];
let t: Vec<_> = arr.iter().tail();
println!("Tail: {:?}", t);
// But more interestingly, instead of:
let split = "".split("@");
let vec:Vec<&str> = split.skip(1).collect();
println!("Before: {:?}", vec);
// you can now do:
let tail: Vec<&str> = "".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