Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. // Generates the first 10 odd squares, lazily.
  2. // [1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
  3. // That is, it takes an infinite sequence of squares and
  4. // checks if each subsequent one is odd, until it finds
  5. // ten that are.
  6. fn main() {
  7. let vector: Vec<_> = (1..) // Instructions to produce
  8. // integers forever
  9.  
  10. .map(|x| x * x) // This is now instructions for
  11. // creating an infinite sequence
  12. // of squares
  13.  
  14. .filter(|x| x % 2 != 0) // This is now instructions
  15. // for an infinite sequence
  16. // of _odd_ squares
  17.  
  18. .take(10) // This is now instructions to produce a
  19. // sequence of the first 10 odd squares
  20.  
  21. .collect(); // Only now does the actual work occur;
  22. // the instructions are executed and a
  23. // vector is populated with the results.
  24.  
  25. println!("{:?}", vector);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement