File: demo.rs

package info (click to toggle)
rust-inplace-vec-builder 0.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 124 kB
  • sloc: makefile: 2
file content (36 lines) | stat: -rw-r--r-- 759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use inplace_vec_builder::InPlaceVecBuilder;

struct Collection {
    elements: Vec<u32>,
}

impl Collection {
    fn op(&mut self) {
        let mut res = self
            .elements
            .iter()
            .filter(|x| **x > 5)
            .map(|x| *x * 2)
            .chain(std::iter::once(123))
            .collect();
        std::mem::swap(&mut self.elements, &mut res);
    }

    fn inplace_op(&mut self) {
        let mut t = InPlaceVecBuilder::from(&mut self.elements);
        while let Some(elem) = t.pop_front() {
            if elem > 5 {
                t.push(elem * 2);
            }
        }
        t.push(123);
    }
}

fn main() {
    let mut x = Collection {
        elements: Vec::new(),
    };
    x.op();
    x.inplace_op();
}