I am getting this issue, as the variables I am deconstructing are borrowed(?) and can't be used in another method. This sounds like a very typical use case but I am not sure how to solve it.
`➜ hello_cargo git:(master) ✗ cargo build
Compiling hello_cargo v0.1.0 (/Users/johnny/Projects/hello_cargo)
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:24:39
|
24 | let DBAndCFs { db: _, cfs } = self.db.lock().as_ref().unwrap();
| ^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
25 | cfs.len()
| --------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
`
Here is the code that generates this issue:
use parking_lot::Mutex;
struct CF {
inner: *mut i32,
}
struct DBAndCFs {
db: i32,
cfs: Vec<CF>,
}
struct DB {
db: Mutex<Option<DBAndCFs>>,
}
impl DB {
pub fn open() -> DB {
DB {
db: Mutex::new(Some(DBAndCFs{ db: 0, cfs: Vec::new() } )),
}
}
pub fn get(&self) -> usize {
let DBAndCFs { db: _, cfs } = self.db.lock().as_ref().unwrap();
cfs.len()
}
}
fn main() {
let db = DB::open();
print!("{}", db.get());
}