我大致有以下代码:
let val = util::replace(&mut self.some_field[i], self.some_method());
它失败并显示以下消息:
unrelated.rs:61:65: 61:70 error: cannot borrow `*self` as immutable because it is also borrowed as mutable
unrelated.rs:61 let val = util::replace(&mut self.some_field[i], self.some_method());
^~~~~
unrelated.rs:61:36: 61:62 note: second borrow of `*self` occurs here
unrelated.rs:61 let val = util::replace(&mut self.some_field[i], self.some_method());
^~~~~~~~~~~~~~~~~~~~~~~
我可以通过以下代码解决此问题:
let temp = self.some_method();
let val = util::replace(&mut self.some_field[i], temp);
但为什么会失败?可变指针和不可变指针的作用域是不同的,它们是不同的表达式。对我来说,这看起来像是一种错误,但我只是想确保我没有在这里遗漏任何东西。