我有以下无法编译的代码:
struct Inner {
val: i32,
}
struct Outer {
obj: Inner,
}
fn main() {
let inner_instance = Inner { val: 123 };
let outer_instance = Outer {
obj: inner_instance,
};
outer_instance.obj.val = 456;
}
error[E0594]: cannot assign to `outer_instance.obj.val`, as `outer_instance` is not declared as mutable
--> src/main.rs:15:5
|
11 | let outer_instance = Outer {
| -------------- help: consider changing this to be mutable: `mut outer_instance`
...
15 | outer_instance.obj.val = 456;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign
到目前为止,一切都很好。我将outer_instance
更改为可变的:
let mut outer_instance = Outer { /* ... */ };
,现在代码可以编译了。看来内部结构的可变性(枚举也是吗?)由外部结构的可变性决定。这是正确的结论吗?