Hi all,
With controlling scope so important in Rust, esp for things like MutexGuards, Iām finding the brace syntax somewhat cumbersome.
Other languages like Ocaml have let ... in ... syntax for declaring variables with limited scope, and it seems to me that it would also be useful in Rust. Specifically, something like:
let locked = thing.lock().unwrap() in { locked.use() + locked.otheruse() }
which seems a lot more readable than:
{
let locked = thing.lock().unwrap();
locked.use() + locked.otheruse()
}
This form of let could also be an expression which has the value of the in clause, allowing:
let val = let locked = thing.lock().unwrap() in locked.use() + locked.otheruse();
though this might cause ambiguities with if let or while let, depending on how much look ahead the parser has. Or perhaps just require let .. in to be parenthesized to break the ambiguity.
Because let pattern = expr expr is currently a syntax error, the in keyword isnāt strictly needed, but it seems like it would be too easy to accidentally use this form without it:
let x = foo() // forgotten ;
something(x); // OK, x in scope
some_other_thing(x); // error, x not in scope (!?)
Thoughts?
This seems like a pretty obvious extension so Iām sure it has been considered before; if so, I wonder if it was outright rejected, or just put on the backburner.