我想更改另一个结构中数组中结构的值:
struct Foo<'a> {
bar: &'a [&'a mut Bar]
}
struct Bar {
baz: u16
}
impl<'a> Foo<'a> {
fn add(&mut self, x: u16) {
self.bar[0].add(x);
}
}
impl Bar {
fn add(&mut self, x: u16) {
self.baz += x;
}
}
这给出了一个错误:
error[E0389]: cannot borrow data mutably in a `&` reference
--> src/main.rs:11:9
|
11 | self.bar[0].add(x);
| ^^^^^^^^^^^ assignment into an immutable reference
如何解决这个例子?
您可以使用其他方法修复编译错误mut
:
bar: &'a [&'a mut Bar]
to bar: &'a mut [&'a mut Bar]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)