fn main() {
let lock = std::sync::Mutex::new(10);
if let Ok(_) = lock.try_lock() {}
}
Error:
error[E0597]: `lock` does not live long enough
--> src/main.rs:3:20
|
3 | if let Ok(_) = lock.try_lock() {}
| ^^^^-----------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
4 | }
| -
| |
| `lock` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::result::Result<std::sync::MutexGuard<'_, i32>, std::sync::TryLockError<std::sync::MutexGuard<'_, i32>>>`
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.
error: could not compile `tmp`.
To learn more, run the command again with --verbose.
I'm surprised that this does not compile!
The error message suggests adding a semicolon after the expression, but it's not obvious to me what exactly the expression is and where the semicolon should go. A suggestion on where to place the semicolon would be helpful.
The solution is this and compiles just fine:
fn main() {
let lock = std::sync::Mutex::new(10);
if let Ok(_) = lock.try_lock() {};
}
I tried compiling this code:
Error:
I'm surprised that this does not compile!
The error message suggests adding a semicolon after the expression, but it's not obvious to me what exactly the expression is and where the semicolon should go. A suggestion on where to place the semicolon would be helpful.
The solution is this and compiles just fine: