The following code does not compile (playground):
#![allow(unused)]
use std::cell::RefCell;
enum Token {
A,
B,
}
struct Thing {
c: Token,
}
fn main() {
let a = RefCell::new(Thing { c: Token::A });
if let Token::A = a.borrow().c {
//Error!
}
}
It fails with this error:
error[E0597]: `a` does not live long enough
--> src/main.rs:16:23
|
16 | if let Token::A = a.borrow().c {
| ^ borrowed value does not live long enough
...
19 | }
| - `a` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
If I add anything after the statement, even a single ;
then it works fine:
if let Token::A = a.borrow().c {
}; // Ok
I assume that it is because the value of the if
is used as the return of main
, so the borrow in the if let
is somehow extended. But that should not happen, it is just ()
! Or is there something I'm not understanding?
BTW, the same thing happens with:
match a.borrow().c {
_ => ()
}