Isn't
if let pat = expr1 { expr2 }
just syntactic sugar for match expr1 {
pat => { expr2 }
_ => { }
meaning the type of expr2 must be unit, and thusly if let is always of unit type?Of course you could. If there is no `else` (which there can be, fwiw, just as the else case is optional in a regular if/else expression), the value of the None case is Unit: http://is.gd/Zdgc3D
I initially considered writing that, but it depends on what the original poster means by "expression-based language" and they haven't made that clear: in Rust, a function is a sequence of statements. An assignment is a statement for instance, not an expression. To me an expression-based language is something like Haskell or Elm, not only do you have ubiquitous expressions (which Rust does have) there's really no such thing as a statement, a program (or function) simply is an expression (possibly with multiple components) this Rust:
let a = 1;
let b = 2;
a + b
is 3 statements. The equivalent Elm or Haskell is a single expression: let
a = 1
b = 2
in
a + bThe original commenter seems to mean 'purely functional' rather than 'expression based'. OCaml uses `let in`, but still has side effects.
When I first saw Option it seems like a great way to force handling of the None case, either explicitly in place, or by passing on a mapped option as a return value (monadic composition make this a nice thing).
This resembles how exceptions are handled in most procedural languages, either you catch it and handle it, or you don't, and someone else gets to do it further up the call stack.
The example on that page looks like the equivalent of an empty catch block. It's almost certainly a bad idea that will bite you in the ass some day.