0

In the below, is it possible to create foo_bars without moving/taking ownership of foos, or cloning it. I need to be able to use foos after creating foo_bars.

struct Foo<'a> {
    bar: &'a str,
}

fn main() {
    let foos = vec![Foo { bar: "one" }, Foo { bar: "two" }, Foo { bar: "three" }];
    let foo_bars: Vec<&str> = foos.into_iter().map(|foo| foo.bar).collect();
    println!("{}", foo_bars[1]); // should print 'two'
    println!("{}", foos[0].bar); // should print 'one'
}
Max888
  • 2,161
  • 16
  • 31

0 Answers0