fork download
  1. use std::io::stdin;
  2.  
  3. fn read_line() -> String {
  4. let mut buf = String::new();
  5. stdin().read_line(&mut buf).unwrap();
  6. buf.trim().to_string()
  7. }
  8.  
  9. fn read_int() -> i32 {
  10. read_line().parse().unwrap()
  11. }
  12.  
  13. #[derive(Debug)]
  14. struct Foo {
  15. value: i32
  16. }
  17.  
  18. impl From<i32> for Foo {
  19. fn from(x: i32) -> Foo {
  20. Foo{ value: x }
  21. }
  22. }
  23.  
  24. fn main() {
  25. let xs: Vec<Foo> = vec![1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]
  26. .into_iter().map(Foo::from).collect();
  27.  
  28. let mut vs: Vec<&Foo> = xs.iter().collect();
  29.  
  30. println!("{:?}", vs);
  31.  
  32. let n = read_int();
  33.  
  34. for _ in 0..n {
  35. let line = read_line();
  36. let mut iter = line.split_whitespace();
  37. let op = iter.next().unwrap();
  38. let value: i32 = iter.next().unwrap().parse().unwrap();
  39. println!("{} {}", op, value);
  40. vs = vs.into_iter()
  41. .filter(|v| match op {
  42. "=" => v.value == value,
  43. "!" => v.value != value,
  44. ">" => v.value > value,
  45. "<" => v.value < value,
  46. _ => unreachable!(),
  47. })
  48. .collect();
  49. println!("{:?}", vs);
  50. }
  51.  
  52. }
Success #stdin #stdout 0s 4480KB
stdin
5
< 7
! 5
> 2
< 5
= 3
stdout
[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 }]