Mutex lifetimes when use match


Can someone explain me on why the compiler complain about m is a ''borrowed value does not live long enough'':

use std::sync::Mutex;

fn main() {
    let m: Mutex<Option<i32>> = Mutex::new(None);
    match m.lock(){
        Ok(_) => {}
        _ => {}

This has to do with interplay between tail expressions in a block and temporaries - this reddit thread has a good discussion, so I won't repeat it here.

You can add a semicolon to the match to turn it into a statement. It also turns out Rust 2018 edition yields a much nicer/helpful error message:

error[E0597]: `m` does not live long enough
  --> src/
6  |     match m.lock(){
   |           ^-------
   |           |
   |           borrowed value does not live long enough
   |           a temporary with access to the borrow is created here ...
10 | }
   | -
   | |
   | `m` 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<'_, std::option::Option<i32>>, std::sync::PoisonError<std::sync::MutexGuard<'_, std::option::Option<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.

Ok, good to know, thanks.