Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct Widget {
- name: String,
- id: i32,
- r#type: String
- }
- fn main() {
- let mut list = vec![];
- list.push(Widget {
- name: "Fred".to_string(),
- id: 1,
- r#type: "One".to_string()
- });
- list.push(Widget {
- name: "Bob".to_string(),
- id: 2,
- r#type: "Two".to_string()
- });
- list.push(Widget {
- name: "Foo".to_string(),
- id: 3,
- r#type: "Three".to_string()
- });
- list.push(Widget {
- name: "Bar".to_string(),
- id: 4,
- r#type: "One".to_string()
- });
- let left = |w: &Widget| w.id <= 2;
- let right = |w: &Widget| w.name.starts_with("F");
- let and = |w: &Widget| left(w) && right(w);
- let results: Vec<Widget> = list.into_iter().filter(and).collect();
- for item in results {
- println!("{:?}", item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement