[Pitch] Last expression as return value

Here's an example I originally posted in the SE-0380 proposal:

I think one of the biggest issues with Rust's implementation is the impact to readability when you start nesting:

fn do_a_thing() -> u32 {
  // <lotsa code>
  ...
  if some_condition { // This `if` statement is the last expression in the function and will implicitly provide the return value
    // <more code>
    ...
    match some_var { // This `match` statement is the last expression in the `if` block and will implicitly provide the result for the `if` statement
      0 => 100, // This value provides the result for the `match` statement and ultimately becomes the return value of the function - is it obvious?
      1 => {
        // <yet mode code>
        ...
        10 // A second return value
      }
      _ => 1, // Another return value
    }
  } else {
    // <probably more code>
    ...
    0 // One last return value
  }
}

The lack of explicit return makes this incredibly confusing to work out where the return values actually are. This would equally be a problem if instead of being return values they were assignments to var x = if some_condition {.
To be fair, the nesting issue still exists even for single-line expressions, but it is far less pronounced. At the end of the day it is up to the user to write readable code but I feel like Rust encourages patterns like this, particularly if you use clippy.

11 Likes