Created
March 2, 2018 13:05
-
-
Save msehnout/903085ac628be614f538cac3f9708dfc to your computer and use it in GitHub Desktop.
Simple flat_map example - filtering data from list of structs
This file contains 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
#[derive(Debug)] | |
struct Task { | |
b: u8, | |
c: String, | |
} | |
#[derive(Debug)] | |
struct Day { | |
a: u8, | |
tasks: Vec<Task>, | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let t1 = Task { b: 1, c: "ahoj".to_string() }; | |
let t2 = Task { b: 2, c: "ahojjjj".to_string() }; | |
let t3 = Task { b: 2, c: "bbbbjjj".to_string() }; | |
let t4 = Task { b: 2, c: "ccccjjj".to_string() }; | |
let day1 = Day { a: 3, tasks: vec![t1, t3, t4]}; | |
let day2 = Day { a: 4, tasks: vec![t2]}; | |
let days = vec![day1, day2]; | |
let tasks: Vec<&Task> = days.iter() | |
.flat_map(|x| &x.tasks) | |
.filter(|x| x.b == 2) | |
.collect(); | |
println!("{:?}", tasks); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment