No código abaixo, estou tendo um problema relacionado ao verificador de empréstimo se eu comentar a println!
declaração após a expressão de correspondência. E para corrigir esse problema, o compilador está me pedindo para colocar um ponto e vírgula no final da match
expressão, o que resolve o problema, mas não consegui entender a necessidade disso.
use std::sync::{Arc, Mutex};
#[derive(Default)]
struct A {
b: Arc<Mutex<B>>,
}
#[derive(Default)]
struct B {
c: Arc<Mutex<C>>,
}
#[derive(Default)]
struct C {
d: i32,
}
fn main() {
let a = A::default();
match a.b.lock().unwrap().c.lock().unwrap().d {
0 => println!("zero"),
_ => println!("non-zero"),
}
// uncommenting the below line makes borrow checker happy
// println!("{}", a.b.lock().unwrap().c.lock().unwrap().d);
}
Erro
error[E0597]: `a.b` does not live long enough
--> src/main.rs:21:11
|
19 | let a = A::default();
| - binding `a` declared here
20 |
21 | match a.b.lock().unwrap().c.lock().unwrap().d {
| ^^^----------------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
...
27 | }
| -
| |
| `a.b` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
|
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
|
24 | };
| +
For more information about this error, try `rustc --explain E0597`.