Pre-RFC: local bindings for let-chains with Logical-Or

This is small proposal, which could extend of using let-chains to "Or"-expressions and match-if conditions

Let we wish to write an error-less statement:

let x = if 
          let Some(y) = expr1 
          && y>3 
      || 
          let Some(z) = expr2 
          && z<50 
  {5} 
  else 
  {6};

Now this is forbidden.

But if we allow local binding in conditional let, it would be Ok

let x = if 
          let Some(local y) = expr1 
          && y>3 
      || 
          let Some(local z) = expr2 
          && z<50 
  {5} 
  else 
  {6};

local-mark show us, that this binding never escapes from condition! But they also are limited by ||-or expressions and !-not expressions.

Together with local-mark we could extend to use let-chains inside match-if conditions. Now match-if forbids to bind variables.

pub enum Value {
  Int8(i8),
  Int16(i16),
  Int32(i32),
}
match value {
    Value::Int8(local  v1) if let v = i32::from(v1) 
  | Value::Int16(local v2) if let v = i32::from(v2) 
  | Value::Int32(v)
        => { do_something() }
}

Syntax

Changing syntax is minimal:

IdentifierPattern :
      local? ref? mut? IDENTIFIER (@ PatternNoTopAlt ) ?

Using local-mark in non-conditional expressions is a compile error

1 Like