Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generates the first 10 odd squares, lazily.
- // [1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
- // That is, it takes an infinite sequence of squares and
- // checks if each subsequent one is odd, until it finds
- // ten that are.
- fn main() {
- let vector: Vec<_> = (1..) // Instructions to produce
- // integers forever
- .map(|x| x * x) // This is now instructions for
- // creating an infinite sequence
- // of squares
- .filter(|x| x % 2 != 0) // This is now instructions
- // for an infinite sequence
- // of _odd_ squares
- .take(10) // This is now instructions to produce a
- // sequence of the first 10 odd squares
- .collect(); // Only now does the actual work occur;
- // the instructions are executed and a
- // vector is populated with the results.
- println!("{:?}", vector);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement