Closed
Description
I've ran into a borrowing issue using try_borrow() in an if let statement.
The problem occurs when it's the last expression of the scope and is resolved by making it a statement.
I'm not sure if this is a bug or limitation in the borrow checker, but either way the error message is confusing.
fn does_not_compile() {
let container = RefCell::new(0);
if let Ok(_) = container.try_borrow() { }
}
fn compiles() {
let container = RefCell::new(0);
if let Ok(_) = container.try_borrow() { };
}
rustc 1.16.0 (30cf806 2017-03-10)
error:container
does not live long enough
--> :6:1
|
5 | if let Ok(_) = container.try_borrow() { }
| --------- borrow occurs here
6 | }
| ^container
dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are createderror: aborting due to previous error
Activity
Havvy commentedon Apr 24, 2017
My own findings show that this does not happen with
if
, only withif let
.Playpen
Furthermore, the Rust reference says that An
if let
expression is semantically equivalent to anif
expression, so the mismatch between the first and second functions in this comment has to be a bug.eddyb commentedon Apr 25, 2017
Closing as duplicate of #21114 (comment). The reference is just wrong, only syntax is similar.