鉴于以下计划:
struct Data {
pub items: Vec<&'static str>,
}
trait Generator {
fn append(&mut self, s: &str) {
self.output().push_str(s);
}
fn data(&self) -> &Data;
fn generate_items(&mut self) {
for item in self.data().items.iter() {
match *item {
"foo" => self.append("it was foo\n"),
_ => self.append("it was something else\n"),
}
}
}
fn output(&mut self) -> &mut String;
}
struct MyGenerator<'a> {
data: &'a Data,
output: String,
}
impl<'a> MyGenerator<'a> {
fn generate(mut self) -> String {
self.generate_items();
self.output
}
}
impl<'a> Generator for MyGenerator<'a> {
fn data(&self) -> &Data {
self.data
}
fn output(&mut self) -> &mut String {
&mut self.output
}
}
fn main() {
let data = Data {
items: vec!["foo", "bar", "baz"],
};
let generator = MyGenerator {
data: &data,
output: String::new(),
};
let output = generator.generate();
println!("{}", output);
}
尝试编译时会产生以下错误:
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/main.rs:15:26
|
13 | for item in self.data().items.iter() {
| ---- - immutable borrow ends here
| |
| immutable borrow occurs here
14 | match *item {
15 | "foo" => self.append("it was foo\n"),
| ^^^^ mutable borrow occurs here
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/main.rs:16:22
|
13 | for item in self.data().items.iter() {
| ---- - immutable borrow ends here
| |
| immutable borrow occurs here
...
16 | _ => self.append("it was something else\n"),
| ^^^^ mutable borrow occurs here
构造代码的正确方法是什么,以便在迭代不可变字段output
时可以写入可变字段data
?假设通过Generator
特征的间接用于与其他结构共享类似的逻辑,因此从特征的默认方法实现中访问MyStruct
的字段需要通过这样的访问器方法来完成。
答案 0 :(得分:6)
这是Rust的一个常见问题;解决它的典型方法是替换舞蹈。这涉及使更多的数据和方法使用可变引用:
struct Data {
pub items: Vec<&'static str>,
}
trait Generator {
fn append(&mut self, s: &str) {
self.output().push_str(s);
}
fn data(&mut self) -> &mut Data;
fn generate_items(&mut self) {
// Take the data. The borrow on self ends after this statement.
let data = std::mem::replace(self.data(), Data { items: vec![] });
// Iterate over the local version. Now append can borrow all it wants.
for item in data.items.iter() {
match *item {
"foo" => self.append("it was foo\n"),
_ => self.append("it was something else\n"),
}
}
// Put the data back where it belongs.
std::mem::replace(self.data(), data);
}
fn output(&mut self) -> &mut String;
}
struct MyGenerator<'a> {
data: &'a mut Data,
output: String,
}
impl<'a> MyGenerator<'a> {
fn generate(mut self) -> String {
self.generate_items();
self.output
}
}
impl<'a> Generator for MyGenerator<'a> {
fn data(&mut self) -> &mut Data {
self.data
}
fn output(&mut self) -> &mut String {
&mut self.output
}
}
fn main() {
let mut data = Data {
items: vec!["foo", "bar", "baz"],
};
let generator = MyGenerator {
data: &mut data,
output: String::new(),
};
let output = generator.generate();
println!("{}", output);
}
要意识到的是编译器正确抱怨。想象一下,如果调用output()
具有改变由data()
的返回值引用的事物的副作用那么你在循环中使用的迭代器可能会失效。你的特质函数有隐式契约,他们没有做那样的事情,但没有办法检查这个。因此,您唯一可以做的就是暂时取消对数据的完全控制。
当然,这种模式打破了放松的安全性;循环中的恐慌将使数据移出。
答案 1 :(得分:4)
假设通过
Generator
特征的间接用于与其他结构共享类似的逻辑,因此从特征的默认方法实现中访问MyStruct
字段需要通过像这样的访问器方法来完成。
然后它是不可能的。
当编译器直接看到这样的字段时,它会识别对不同字段的访问。它不会破坏抽象边界来窥视所调用的函数。
有关于在方法上添加属性的讨论,具体提到哪个字段是通过哪种方法访问的:
但是......这适用于非虚拟方法。
对于 trait ,这会变得更加复杂,因为特征没有字段,并且每个实现者可能有不同的字段集!
那么现在呢?
您需要更改代码:
append
方法的可变性,迫使用户使用内部可变性答案 2 :(得分:3)
您可以使用RefCell
:
RefCell使用Rust的生命周期来实现“动态借用”,a 可以声称临时,排他,可变访问的过程 内在的价值。 RefCells的借位在“运行时”被跟踪, 不像Rust的原始引用类型,它们是完全跟踪的 静态地,在编译时。因为RefCell借用它是动态的 可以尝试借用已经可变的值 借来的;当发生这种情况时,会导致线程恐慌。
use std::cell::{RefCell, RefMut};
struct Data {
pub items: Vec<&'static str>,
}
trait Generator {
fn append(&self, s: &str) {
self.output().push_str(s);
}
fn data(&self) -> &Data;
fn generate_items(&self) {
for item in self.data().items.iter() {
match *item {
"foo" => self.append("it was foo\n"),
_ => self.append("it was something else\n"),
}
}
}
fn output(&self) -> RefMut<String>;
}
struct MyGenerator<'a> {
data: &'a Data,
output: RefCell<String>,
}
impl<'a> MyGenerator<'a> {
fn generate(self) -> String {
self.generate_items();
self.output.into_inner()
}
}
impl<'a> Generator for MyGenerator<'a> {
fn data(&self) -> &Data {
self.data
}
fn output(&self) -> RefMut<String> {
self.output.borrow_mut()
}
}
fn main() {
let data = Data {
items: vec!["foo", "bar", "baz"],
};
let generator = MyGenerator {
data: &data,
output: RefCell::new(String::new()),
};
let output = generator.generate();
println!("{}", output);
}