I have 2 functions:
// A simple struct
struct S {
w: u8,
h: u8,
x: Vec<u8>,
y: Vec<u8>,
}
// Implementation of the struct S
impl S {
// Seems to work
fn new(_w: u8, _h: u8, _x: &Vec<u8>, _y: &Vec<u8>) -> S {
S {
w: _w,
h: _h,
x: _x.clone(),
y: _y.clone(),
}
}
fn calc(&mut self) {
let mut min_x = self.x.iter().min().unwrap();
let mut max_x = self.x.iter().max().unwrap();
let mut min_y = self.y.iter().min().unwrap();
let mut max_y = self.y.iter().max().unwrap();
// Here's the line that gives the error
self.x = self.x
.iter()
.map(|v| norm_value(*v, *min_x, *max_x, 0, self.w))
.collect();
}
}
fn norm_value<A, B, C, D, E>(_: A, _: B, _: C, _: D, _: E) -> ! { panic!() }
newmakes a newSobject. This seems to work, but correct me if I did something horribly wrong and just happens to work.calctries to modify the membersxandy.
The compiler reports this error:
error[E0506]: cannot assign to `self.x` because it is borrowed
--> src/main.rs:28:9
|
22 | let mut min_x = self.x.iter().min().unwrap();
| ------ borrow of `self.x` occurs here
...
28 | / self.x = self.x
29 | | .iter()
30 | | .map(|v| norm_value(*v, *min_x, *max_x, 0, self.w))
31 | | .collect();
| |______________________^ assignment to borrowed `self.x` occurs here
Where did I borrow self.x? I am new to Rust but things like this make no sense.