【发布时间】:2016-10-07 13:34:28
【问题描述】:
我有包含变量的枚举:
enum Asymmetric {
One(i32),
Two(i32, i32),
}
我只想更改现有枚举的一个字段,而不重新分配整个枚举。我的代码(playground):
// Does not compile
fn main() {
let two = Asymmetric::Two(4, 5);
let mut vec = vec![two];
foo(&mut vec[0]);
}
fn foo(baa: &mut Asymmetric) {
match baa {
&mut Asymmetric::Two(x0, x1) => {
x0 = 6;
}
_ => {}
}
}
这会导致这个错误:
error[E0384]: re-assignment of immutable variable `x0`
--> src/main.rs:16:13
|
15 | &mut Asymmetric::Two(x0, x1) => {
| -- first assignment to `x0`
16 | x0 = 6;
| ^^^^^^ re-assignment of immutable variable
【问题讨论】: