【发布时间】:2020-04-25 08:41:16
【问题描述】:
为什么以下内容无效,我应该怎么做才能使其正常工作?
struct Foo;
impl Foo {
fn mutable1(&mut self) -> Result<(), &str> {
Ok(())
}
fn mutable2(&mut self) -> Result<(), &str> {
self.mutable1()?;
self.mutable1()?;
Ok(())
}
}
这段代码产生:
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/lib.rs:10:9
|
8 | fn mutable2(&mut self) -> Result<(), &str> {
| - let's call the lifetime of this reference `'1`
9 | self.mutable1()?;
| ---- - returning this value requires that `*self` is borrowed for `'1`
| |
| first mutable borrow occurs here
10 | self.mutable1()?;
| ^^^^ second mutable borrow occurs here
已经有很多问题存在相同的错误,但我不能用它们来解决这个问题,因为它是由? 提供的隐式返回导致问题,没有? 代码编译成功,但是警告。
【问题讨论】:
标签: rust borrow-checker