Hi, I am learning Rust and writing my very first lines of code. I want to create a small particles simulator program, I have this:
#[derive(Debug)]
struct Particle {
mass: f64,
interactions: Vec::<&Particle>, // I want to store references to other particles here.
}
fn main() {
let mut A = Particle {
mass: 1.,
interactions: Vec::new(),
};
let mut B = Particle {
mass: 2.,
interactions: Vec::new(),
};
A.interactions.push(&B);
B.interactions.push(&A);
}
Inside Particle::interactions
I want to store a list of other particles. Since I will not modify these particles from there, they should be references to the other particles. I thought this would work, but I get
--> src/main.rs:4:25
|
4 | interactions: Vec::<&Particle>, // I want to store references to other particles here.
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
2 ~ struct Particle<'a> {
3 | mass: f64,
4 ~ interactions: Vec::<&'a Particle>, // I want to store references to other particles here.
|
I tried following the recommendation of the compiler adding the 'a
(which I still don't know what it means) but then I get other errors.
How could I achieve having a list of references to other particles inside each particle?