Dear Rusties,

  I am currently writing in C++ code that involves long strings of `if
... else if ... else if ... else if ...` and it dawns to me that we can
certainly sightly tweak the syntax of Rust to make such code much nicer.

Consider the following extract:

if (f()) {
  // ...
} else if (g()) {
 // ...
} else if (h()) {
 // ...
} else {
 // ...
}

It is quite easy to get lost in such long strings of `if...else if...`.
Fortunately, in Rust, this can be rewritten with more structure as

alt true {
  true if f(): ...
  true if g(): ...
  true if h(): ...
  _: ...
}

or

alt true {
  _ if f(): ...
  _ if g(): ...
  _ if h(): ...
  _: ...
}

However, the pattern-matching on `true` is a little confusing, and we
could certainly make it nicer by allowing `alt` expressions with no
condition expression and only the `if` part, as follows:

alt {
  if f(): ...
  if g(): ...
  if h(): ...
  _: ...
}

I believe that this snippet has a more immediately visible structure
than the original and is easier to read, while the syntax tweak is
trivial to compile.


What do you think?

Cheers,
 David

-- 
David Rajchenbach-Teller, PhD
 Performance Team, Mozilla


Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to