I have code similar to this, which would not compile with Rust v < 1.83.0
fn main() {
let result: Result<(), String> = Err("Something went wrong".to_string());
let status = match result {
Ok(()) => "✅", // Success
Err(err) => &format!("❌ {:?}", err), // Failure (this line causes an error)
};
println!("{}", status);
}
Error:
error[E0716]: temporary value dropped while borrowed
--> tests/integration_tests.rs:143:26
|
141 | let status = match result.result {
| ------ borrow later stored here
142 | Ok(()) => "✅", // Success
143 | Err(err) => &format!("❌ {:?}", err), // Failure
| ^^^^^^^^^^^^^^^^^^^^^^-
| | |
| | temporary value is freed at the end of this statement
| creates a temporary value which is freed while still in use
After upgrading to Rust 1.83.0, this code now compiles fine.
I looked through the last few Rust changelogs to look for an explanation, but I can't find anything that seems relevant.
I'm not sure what version of Rust the error message comes from except that it is less than 1.83.0
Can someone point out the change in the rust language which allows this code to compile?
(What happened is that I had this code on on another machine where it was working fine. I sent it over to an other machine with an earlier version of Rust. When I attempted to compile I got the above error. I 'fixed' it by upgrading Rust to the latest stable version.)
#121346 - Propagate temporary lifetime extension into if and match expressions. newly allows temporary lifetime extensions through if
and match
it was introduced in 1.79.0