Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct Widget {
  3. name: String,
  4. id: i32,
  5. r#type: String
  6. }
  7.  
  8.  
  9. fn main() {
  10.  
  11. let mut list = vec![];
  12.  
  13. list.push(Widget {
  14. name: "Fred".to_string(),
  15. id: 1,
  16. r#type: "One".to_string()
  17. });
  18.  
  19. list.push(Widget {
  20. name: "Bob".to_string(),
  21. id: 2,
  22. r#type: "Two".to_string()
  23. });
  24.  
  25. list.push(Widget {
  26. name: "Foo".to_string(),
  27. id: 3,
  28. r#type: "Three".to_string()
  29. });
  30.  
  31. list.push(Widget {
  32. name: "Bar".to_string(),
  33. id: 4,
  34. r#type: "One".to_string()
  35. });
  36.  
  37. let left = |w: &Widget| w.id <= 2;
  38. let right = |w: &Widget| w.name.starts_with("F");
  39. let and = |w: &Widget| left(w) && right(w);
  40.  
  41. let results: Vec<Widget> = list.into_iter().filter(and).collect();
  42.  
  43. for item in results {
  44. println!("{:?}", item);
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement