What is the difference between a let binding and a match binding in a macro_rules!

I've learnt recently that this code:

macro_rules! max {
  ($a:expr, $b:expr) => (match ($a, $b) {
    (a, b) => if a > b { a } else { b }
  });
}

is preferable to this code:

macro_rules! max {
  ($a:expr, $b:expr) => ({
    let a = $a;
    let b = $b;
    if a > b { a } else { b }
  });
}

There's this comment in the implementation of std::dbg!:

// Use of `match` here is intentional because it affects the lifetimes
// of temporaries - https://stackoverflow.com/a/48732525/1063961

In short, there's a difference between one expression and the sequence of statements, from the borrow-checker and drop-checker point of view.

2 Likes

The answer you linked to did not exactly answer my question. The answer below is more relevant in regard to this topic.