You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow evaluation and pattern matching within guards (what Haskell calls "pattern guards") #12830
What Rust calls "pattern guards" are just called "guards" in Haskell. Pattern guards in Haskell allow additional evaluation and a refutable pattern match. If that pattern match fails, it's as if a regular guard returned false. Here's an example, adapted from the HTML5 tokenizer I'm working on.
fn process_char(&mut self, chars: CharSource) {
match self.state {
TagOpen => match chars.next() {
'/' => { self.state = EndTagOpen; }
c if (Some(a) <= c.to_ascii_opt()) => { do_something(a.to_lower()); }
c if other_condition => { ... }
_ => parse_error()
(I'm not advocating for this particular concrete syntax, just trying to get the idea across.)
One can always refactor to avoid the fancy guard, but in general it can produce ugly, hard-to-follow trees of nested matches. In this case I would love to have a single match per tokenizer state which closely follows the specification.
Pattern guards have proven tremendously useful in GHC and were one of the few GHC extensions accepted into Haskell 2010. I think Rust could benefit just as much.
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.
Activity
Aatch commentedon Mar 11, 2014
Hmm, while not ideal, this does work today:
Prints
2;kmcallister commentedon May 23, 2014
I started using something like that in another part of the library. In this style, the example above would become
Which is sort of weird, but easy enough to get used to.
steveklabnik commentedon Jan 21, 2015
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.
This issue has been moved to the RFCs repo: rust-lang/rfcs#680
Auto merge of rust-lang#12830 - hi-rustin:rustin-patch-issue-12717-fi…
Replace local variables signifying "done" or "loop break", use Contro…
Auto merge of rust-lang#12830 - blyxyas:more-controlflow, r=y21,xFredNet