I have a struct that contains a vector of substructs, each requiring some rather large data structure for computations, which may or may not be shared with other substructs. Thus, it makes sense that the superstruct owns these data structures and the substructs only have references. Attempting to write a constructor for this, I'm running into a lifetime error I can't quite decipher.
Minimal non-working example:
use std::collections::HashMap;
struct SuperStruct<'a> {
hash_map: HashMap<String, Vec<f64>>,
substructs: Vec<SubStruct<'a>>,
}
impl<'a> SuperStruct<'a> {
fn new(vectors: Vec<Vec<f64>>) -> SuperStruct<'a> {
let mut substructs = vec![];
let mut hash_map = HashMap::new();
let mut k = 0;
for vector in vectors {
substructs.push(SubStruct { reference: &vector });
hash_map.insert(format!("vec_{}", k), vector);
k += 1;
}
SuperStruct{ hash_map, substructs }
}
}
struct SubStruct<'a> {
reference: &'a Vec<f64>,
}
fn main() {
// println!("Hello");
}
Trying to compile this results in the following error:
error[E0597]: `vector` does not live long enough
--> moo.rs:15:53
|
15 | substructs.push(SubStruct { reference: &vector });
| ^^^^^^ does
not live long enough
...
18 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on
the impl at 8:1...
--> moo.rs:8:1
|
8 | / impl<'a> SuperStruct<'a> {
9 | | fn new(vectors: Vec<Vec<f64>>) -> SuperStruct<'a> {
10 | | let mut substructs = vec![];
11 | | let mut hash_map = HashMap::new();
... |
20 | | }
21 | | }
| |_^
I bet I'm missing something really obvious, but what is it?