Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2018 04:25
Show Gist options
  • Save anonymous/04596264970b02cf1f4117b0d58d720c to your computer and use it in GitHub Desktop.
Save anonymous/04596264970b02cf1f4117b0d58d720c to your computer and use it in GitHub Desktop.
Rust code shared from the playground
/*
Iterators and all functions that operates on iterators, are lazy by default.
Executes on demand.
Here in we have `println` that executes the lazy expression that is `largest`.
*/
fn main() {
let a = [5,2,3,1,5,7,3];
let mut index = 0;
let largest = a.iter()
.cloned()
.enumerate()
.inspect(|&(x, y)| println!("about to filter: {} and {}", x, y))
.fold(0, |check, (x, y)| {
if y > check {
index = x;
y
} else {
check
}
});
println!("largest is {} occurs at {}", largest, index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment