This question already has an answer here:
- Cannot move out of borrowed content when unwrapping 1 answer
I want to iterate over a vector using .iter_mut()
and .map()
:
fn calculate_distances(planes : &mut Vec<Aeroplane>, x: f64, y: f64) {
fn calculate_distance(x1: &f64, y1: &f64, x2: &f64, y2: &f64) -> f6 { ... }
planes.iter_mut().map(|a| if a.position.is_some() {
let pos: &Position = &a.position.unwrap();
a.distance = Some(calculate_distance(&x, &y, &pos.latitude, &pos.longitude));
});
}
Aeroplane
contains instance of my Position
struct:
struct Position {
latitude: f64,
longitude: f64,
}
In my understanding I'm just borrowing the position information not moving out anything, but the borrow-checker refuses my code:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:145:31
|
4 | let pos: &Position = &a.position.unwrap();
| ^ cannot move out of borrowed content
Where is my mistake?