- Feature Name: More use of pipe in match.
- Start Date: 2017-01-16
Summary
this RFC proposes allowing the |
operator to be used within patterns in match statements, to allow for pattern matching with less boilerplate.
Detailed Design
The current pattern matching in match
statements is very powerful, however there is a lot of boilerplate code needed
to fully take advantage of it. Consider a situation where you are building a state machine that is iterating through
chars_indices
, and when you see ' '
, '\n'
, '\r'
or '\f'
, you want to change the state. Currently your match
statement would look something like the following example. There is no great way of reducing that boilerplate, if anything that boilerplate only grows worse as you have more cases, and bigger tuples.
match iter.next() {
Some(_, ' ') | Some(_, '\n') | Some(_, '\r') | Some(_, '\f') => {
// Change state
}
Some(index, ch) => {
// Look at char
}
None => return Err(Eof),
}
The solution to this would be to allow for |
to be used within tuples. This will significantly reduce how much boilerplate is required to have conditional matches with tuples.
match iter.next() {
Some(_, ' ' | '\n' | '\r' | '\f') => {
// Change state
}
Some(index, ch) => {
// Look at char
}
None => return Err(Eof),
}
How We Teach This
I think all that would be required is a few examples, and changing existing ones. The syntax is pretty intuitive.
Drawbacks
None that I can think of.
Alternatives
Keep syntax as is.
Unresolved Questions
None at the moment