-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let-in binding syntax #11514
Comments
This (or something equivalent) can be written with a macro: #[feature(macro_rules)];
macro_rules! bind {
($( $p:pat $(: $t:ty)* = $val:expr );* in $rhs:expr) => {
{
$( let $p $(: $t)* = $val; )*
$rhs
}
}
}
fn main() {
let v = bind!(a: int = 1; b = 2 in a + b);
println!("{}", v);
} Does that appear to cover what you are thinking of? |
Yep, that's pretty much it. It would look a bit ugly when used with a block, but not too bad I guess. Is it too much to add this to syntax? |
The macro could be tuned to allow And yes, to me, this seems like a poster boy for macros/syntax extensions, rather than hard-coding extended syntax into the language. |
It's quite a common feature of Of course, I can work with a macro as well. |
I'm going to close this in favour of macros. Anyone who feels particularly strongly (preferably with a good reason why a macro is insufficient) can comment/reopen. |
…les, r=flip1995 triagebot no-merges: exclude "Rustup"s, add labels rust-lang/triagebot#1720 changelog: none
I thought it would be interesting to be also able to bind a symbol only for the scope of the following expression, much like "let ... in" syntax in Haskell/ML.
So this
would be equivalent to this
Such syntax would also more closely align with
match
, where the symbols are scoped only in that arm of the match.I've felt the need for this trying to write
Some(&1)
and having to fall back binding it separately, but I understand that will be fixed.The text was updated successfully, but these errors were encountered: