-
-
Save trickster/faf50dab5f1ddaadf1e6860fbf4ac6c0 to your computer and use it in GitHub Desktop.
Rust code shared from the playground (Iterators)
This file contains hidden or 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
/* | |
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