borrow checker - Why do recent versions of Rust allow a `match` expression to return a reference to a temporary? - Stack Overflo

admin2025-05-02  1

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.)

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.)

Share Improve this question edited Jan 3 at 9:21 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Jan 2 at 13:57 pnadeaupnadeau 6136 silver badges11 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 16

#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

转载请注明原文地址:http://anycun.com/QandA/1746116315a91896.html