use std::io::stdin; fn read_line() -> String { let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); buf.trim().to_string() } fn read_int() -> i32 { read_line().parse().unwrap() } #[derive(Debug)] struct Foo { value: i32 } impl From<i32> for Foo { fn from(x: i32) -> Foo { Foo{ value: x } } } fn main() { let xs: Vec<Foo> = vec![1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8] .into_iter().map(Foo::from).collect(); let mut vs: Vec<&Foo> = xs.iter().collect(); println!("{:?}", vs); let n = read_int(); for _ in 0..n { let line = read_line(); let mut iter = line.split_whitespace(); let op = iter.next().unwrap(); let value: i32 = iter.next().unwrap().parse().unwrap(); println!("{} {}", op, value); vs = vs.into_iter() .filter(|v| match op { "=" => v.value == value, "!" => v.value != value, ">" => v.value > value, "<" => v.value < value, _ => unreachable!(), }) .collect(); println!("{:?}", vs); } }
5 < 7 ! 5 > 2 < 5 = 3
[Foo { value: 1 }, Foo { value: 2 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 5 }, Foo { value: 6 }, Foo { value: 7 }, Foo { value: 8 }, Foo { value: 1 }, Foo { value: 2 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 5 }, Foo { value: 6 }, Foo { value: 7 }, Foo { value: 8 }] < 7 [Foo { value: 1 }, Foo { value: 2 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 5 }, Foo { value: 6 }, Foo { value: 1 }, Foo { value: 2 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 5 }, Foo { value: 6 }] ! 5 [Foo { value: 1 }, Foo { value: 2 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 6 }, Foo { value: 1 }, Foo { value: 2 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 6 }] > 2 [Foo { value: 3 }, Foo { value: 4 }, Foo { value: 6 }, Foo { value: 3 }, Foo { value: 4 }, Foo { value: 6 }] < 5 [Foo { value: 3 }, Foo { value: 4 }, Foo { value: 3 }, Foo { value: 4 }] = 3 [Foo { value: 3 }, Foo { value: 3 }]