Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 25, 2019 14:54
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 rust-play/b72dc11d0be75bcf694054b819f3e828 to your computer and use it in GitHub Desktop.
Save rust-play/b72dc11d0be75bcf694054b819f3e828 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn iterate_with_iterator<T: std::fmt::Display, I: Iterator<Item = T>>(iter: I) {
for x in iter {
println!("{}", x);
}
}
fn iterate_with_boxed_iterator<'a, T: std::fmt::Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
for x in iter {
println!("{}", x);
}
}
fn iterate_with_deref_boxed_iterator<'a, T: std::fmt::Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
let diter = *iter;
for x in diter {
println!("{}", x);
}
}
fn main() {
let test = String::from("test");
iterate_with_iterator(test.chars());
iterate_with_boxed_iterator(Box::new(test.chars()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment