如何在嵌套结构的实例中更改字段的值?
// Do Not Change - Start
struct Base {
val: String,
}
struct Level1 {
val: Base,
}
struct Level2 {
val: Level1,
}
// Do Not Change - End
fn main() {
let x = Level2 {
val: Level1 {
val: Base {
val: "World".to_string(),
},
},
};
println!(" Hello {}", x.val.val.val);
x.val.val.val = "Moon".to_string();
println!(" Hello {}", x.val.val.val);
}
error[E0594]: cannot assign to field `x.val.val.val` of immutable binding
--> src/main.rs:28:5
|
18 | let x = Level2 {
| - help: make this binding mutable: `mut x`
...
28 | x.val.val.val = "Moon".to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot mutably borrow field of immutable binding
答案 0 :(得分:3)
我强烈建议您返回并重新阅读The Rust Programming Language,尤其是有关variables and mutability的章节。
按照编译器的提示执行操作:
<?php ?> <script> var spans = document.getElementsByTagName("span"); for (var i=0; i < spans.length; i++) { if (spans[i].innerHTML.contains("Archives")) { //is this the "Welcome" span? spans[i].innerHTML = "test string"; //change to new value break; //hop out of the loop, we're done } } </script>
help: make this binding mutable: `mut x`
另请参阅: