Created
March 4, 2016 15:56
-
-
Save anonymous/75dde9ca2f21032a77cf to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
const PLF: &'static &'static str = &"\n"; | |
struct LfIter<I> where I: Iterator { | |
iter: I, | |
lf_every: u32, | |
items: u32 | |
} | |
impl<I> LfIter<I> where I: Iterator { | |
fn new(iter: I, lf_every: u32) -> LfIter<I> { | |
LfIter { | |
iter: iter, | |
lf_every: lf_every, | |
items: 0 | |
} | |
} | |
} | |
impl<'i, I> Iterator for LfIter<I> where I: Iterator<Item=&'i &'i str> { | |
type Item = &'i &'i str; | |
fn next(&mut self) -> Option<Self::Item> { | |
if self.items >= self.lf_every { | |
self.items = 0; | |
Some(PLF) | |
} else { | |
self.items = self.items + 1; | |
self.iter.next() | |
} | |
} | |
} | |
fn main() { | |
let d = ["A", "B", "C", "D", "E", "F", "G"]; | |
let lf_iter = LfIter::new(d.iter(), 3); | |
for l in lf_iter { | |
print!("{}", l); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment