【发布时间】:2018-04-18 10:06:53
【问题描述】:
我有两个功能:
// 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!() }
new创建一个新的S对象。这似乎有效,但如果我做错了什么并且恰好有效,请纠正我。calc尝试修改成员x和y。
编译器报这个错误:
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
我在哪里借到self.x?我是 Rust 新手,但这样的事情毫无意义。
【问题讨论】:
标签: rust ownership borrow-checker