How can I have a reference to a value inside a struct?
I tried to simplified my question as much as possible.
pub struct Point {
x: i32,
y: i32,
}
pub struct Screen<'a> {
points: HashMap<i32, Point>,
aliases: HashMap<&'a str, &'a Point>,
}
impl<'a> Screen<'a> {
pub fn new() -> Screen<'a> {
let p1 = Point{x:1, y:1};
let p2 = Point{x:2, y:2};
let mut points = HashMap::new();
points.insert(1, p1);
points.insert(2, p2);
let mut aliases = HashMap::new();
aliases.insert("p1", &p1); //
aliases.insert("p2", &p2);
Screen {
points: points,
aliases: aliases,
}
}
}
error is:
error[E0382]: borrow of moved value: `p1`
--> src/main.rs:25:30
|
17 | let p1 = Point{x:1, y:2};
| -- move occurs because `p1` has type `Point`, which does not implement the `Copy` trait
...
21 | points.insert(1, p1);
| -- value moved here
...
25 | aliases.insert("p1", &p1);
| ^^^ value borrowed here after move
Here is the playground for this code: Rust Playground
My question might be repeated, but unfortunately I couldn't find the answer by googling.