Hello,
I'm trying to use a struct attribute vector in a thread, with following code:
use std::thread;
fn main() {
let mut handles = Vec::new();
struct Foo {
bar: Vec<Vec<u32>>,
}
let mut bar: Vec<Vec<u32>> = Vec::new();
let mut baz: Vec<u32> = Vec::new();
baz.push(42);
bar.push(baz);
let foo = Foo{bar};
for bar2 in foo.bar.iter() {
for ii in bar2.iter() {
handles.push(thread::spawn(move || {
println!("{:?}{}", bar2, ii);
}));
}
}
for handle in handles {
let _ = handle.join();
}
println!("ok")
}
But foo.bar
borrowed value does not live long enough:
error[E0597]: `foo.bar` does not live long enough
--> src/main.rs:16:17
|
16 | for bar2 in foo.bar.iter() {
| ^^^^^^^-------
| |
| borrowed value does not live long enough
| argument requires that `foo.bar` is borrowed for `'static`
...
29 | }
| - `foo.bar` dropped here while still borrowed
error: aborting due to previous error
Wat is the correct way to iterate over foo.bar
?