| 1 | use inplace_vec_builder::InPlaceVecBuilder; |
| 2 | |
| 3 | struct Collection { |
| 4 | elements: Vec<u32>, |
| 5 | } |
| 6 | |
| 7 | impl Collection { |
| 8 | fn op(&mut self) { |
| 9 | let mut res = self |
| 10 | .elements |
| 11 | .iter() |
| 12 | .filter(|x| **x > 5) |
| 13 | .map(|x| *x * 2) |
| 14 | .chain(std::iter::once(123)) |
| 15 | .collect(); |
| 16 | std::mem::swap(&mut self.elements, &mut res); |
| 17 | } |
| 18 | |
| 19 | fn inplace_op(&mut self) { |
| 20 | let mut t = InPlaceVecBuilder::from(&mut self.elements); |
| 21 | while let Some(elem) = t.pop_front() { |
| 22 | if elem > 5 { |
| 23 | t.push(elem * 2); |
| 24 | } |
| 25 | } |
| 26 | t.push(123); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn main() { |
| 31 | let mut x = Collection { |
| 32 | elements: Vec::new(), |
| 33 | }; |
| 34 | x.op(); |
| 35 | x.inplace_op(); |
| 36 | } |