试着做一些
use std::sync::Arc;
struct Foo(Arc<Bar>);
# [derive(Clone)]
struct Bar;
impl Foo {
fn bar_mut(&mut self) -> &mut Bar {
match Arc::get_mut(&mut self.0) {
Some(bar) => bar,
None => {
let new_bar = Bar::clone(&self.0);
self.0 = Arc::new(new_bar);
Arc::get_mut(&mut self.0).unwrap()
}
}
}
}
fn main() {
Foo(Arc::new(Bar)).bar_mut();
}
但我得到以下错误:
* Executing task: cargo check
Checking testproj v0.1.0 (/home/dspyz/testproj)
error[E0502]: cannot borrow `self.0` as immutable because it is also borrowed as mutable
--> src/main.rs:13:42
|
9 | fn bar_mut(&mut self) -> &mut Bar {
| - let's call the lifetime of this reference `'1`
10 | match Arc::get_mut(&mut self.0) {
| - ----------- mutable borrow occurs here
| _________|
| |
11 | | Some(bar) => bar,
12 | | None => {
13 | | let new_bar = Bar::clone(&self.0);
| | ^^^^^^^ immutable borrow occurs here
... |
16 | | }
17 | | }
| |_________- returning this value requires that `self.0` is borrowed for `'1`
error[E0506]: cannot assign to `self.0` because it is borrowed
--> src/main.rs:14:17
|
9 | fn bar_mut(&mut self) -> &mut Bar {
| - let's call the lifetime of this reference `'1`
10 | match Arc::get_mut(&mut self.0) {
| - ----------- borrow of `self.0` occurs here
| _________|
| |
11 | | Some(bar) => bar,
12 | | None => {
13 | | let new_bar = Bar::clone(&self.0);
14 | | self.0 = Arc::new(new_bar);
| | ^^^^^^ assignment to borrowed `self.0` occurs here
15 | | Arc::get_mut(&mut self.0).unwrap()
16 | | }
17 | | }
| |_________- returning this value requires that `self.0` is borrowed for `'1`
error[E0499]: cannot borrow `self.0` as mutable more than once at a time
--> src/main.rs:15:30
|
9 | fn bar_mut(&mut self) -> &mut Bar {
| - let's call the lifetime of this reference `'1`
10 | match Arc::get_mut(&mut self.0) {
| - ----------- first mutable borrow occurs here
| _________|
| |
11 | | Some(bar) => bar,
12 | | None => {
13 | | let new_bar = Bar::clone(&self.0);
14 | | self.0 = Arc::new(new_bar);
15 | | Arc::get_mut(&mut self.0).unwrap()
| | ^^^^^^^^^^^ second mutable borrow occurs here
16 | | }
17 | | }
| |_________- returning this value requires that `self.0` is borrowed for `'1`
Some errors have detailed explanations: E0499, E0502, E0506.
For more information about an error, try `rustc --explain E0499`.
error: could not compile `testproj` due to 3 previous errors
- 终端进程“货'检'"启动失败(退出代码:101页)。
- 终端将被任务重复使用,按任意键关闭它。
1条答案
按热度按时间zpf6vheq1#
借位检查器并不完美,仍然有一些情况下,它很难确定所涉及的生存期。每个版本都有改进,但目前,它很难弄清楚如何在代码中分配生存期。
它看到函数应该返回一个值
&'0 mut Bar
,并将该生存期'0
赋给Arc::get_mut
的输出。但是,这将意味着,当您随后试图在函数中重用这些值时,所使用的可变借位也将需要在导致错误的函数的持续时间内存在。希望这将在Rust编译器的后续版本中得到修复。这很烦人,但是我们可以通过先检查
None
的情况来避免这个问题。如果运气好的话,编译器应该能够优化它,得到相同的输出。