rust-lang.github.io
...use E::*;
enum E { A, B, C, D }
match foo {
| A
| B
| C
| D => (),
// ^ Gained consistency by having a matching vert.
}
Flexibility in single line matches
It would allow these examples which are all equivalent:
use E::*;
enum E { A, B, C, D }
// A preceding vert
match foo {
| A | B...
RFC 1925
RFC
2017-02-23
~4 min read
rust-lang.github.io
Summary
Better ergonomics for pattern-matching on references.
Currently, matching on references requires a bit of a dance using ref and & patterns:
let x: &Option<_> = &Some(0);
match x {
&Some(ref y) => { ... },
&None => { ... },
}
// or using `*`:
match *x {
Some(ref x) => { ... },
None => { ... },
}
After this RFC, the above form still works, but...
RFC 2005
RFC
2016-08-12
~11 min read
rust-lang.github.io
Summary
Allow if let guards in match expressions.
Motivation
This feature would greatly simplify some logic where we must match a pattern iff some value computed from the match-bound values has a certain form, where said value may be costly or impossible (due to affine semantics) to recompute in...
RFC 2294
RFC
2018-01-15
~5 min read
rust-lang.github.io
Summary
Change array/slice patterns in the following ways:
• Make them only match on arrays ([T; n] and [T]), not slices;
• Make subslice matching yield a value of type [T; n] or [T], not &[T] or &mut [T];
• Allow multiple mutable references to be made to different parts of the...
RFC 495
RFC
2014-12-03
~3 min read
rust-lang.github.io
Summary
Allow attributes on match arms.
Motivation
One sometimes wishes to annotate the arms of match statements with attributes, for example with conditional compilation #[cfg]s or with branch weights (the latter is the most important use).
For the conditional compilation, the work-around is duplicating the whole containing function...
RFC 49
RFC
2014-03-20
~1 min read
rust-lang.github.io
Summary
Extend Rust's pattern matching exhaustiveness checks to cover the integer types: u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize and char.
fn matcher_full(x: u8) {
match x { // ok
0 ..= 31 => { /* ... */ }
32 => { /* ... */ }
33 ..= 255 => { /* ... */ }
}
}
fn matcher_incomplete(x: u8) {
match x { //~ ERROR: non-exhaustive...
RFC 2591
RFC
2018-10-11
~3 min read
rust-lang.github.io
...Like match arm guards, guard patterns restrict another pattern to match only if an expression evaluates to true. The syntax for guard patterns, pat if condition, is compatible with match arm guard syntax, so existing guards can be superceded by guard patterns without breakage.
Motivation
Guard patterns, unlike match arm...
RFC 3637
RFC
2024-05-13
~12 min read
rust-lang.github.io
Summary
The current compiler implements a more expansive semantics for pattern matching than was originally intended. This RFC introduces several mechanisms to reign in these semantics without actually breaking (much, if any) extant code:
• Introduce a feature-gated attribute #[structural_match] which can be applied to a struct or enum...
RFC 1445
RFC
2015-02-06
~16 min read
rust-lang.github.io
Table of contents
• Summary
• Motivation
• Detailed design
• Syntax
• Evolution
• Concrete syntax
• Expansion concerns
• Core API
• RegexBuilder
• Replacer
• quote
• RegexSet
• The bytes submodule
• Drawbacks
• Guaranteed linear time matching
• Allocation
• Synchronization is implicit
• The implementation is complex
• Alternatives
• Big picture
• bytes::Regex
• A regex trait
• Reuse some types
• Unresolved questions
• regex-syntax...
RFC 1620
RFC
2016-05-11
~30 min read
rust-lang.github.io
Summary
Various changes to the match ergonomics rules:
• On edition ≥ 2024, & and &mut patterns only remove a single layer of references.
• On edition ≥ 2024, mut on an identifier pattern does not force its binding mode to by-value.
• On all editions, & patterns can match against &mut references.
• On all editions...
RFC 3627
RFC
2024-05-06
~14 min read
rust-lang.github.io
...Like the for loop before it, this construct can be transformed in a syntax-lowering pass into the equivalent match statement. The expression is given to match and the pattern becomes a match arm. If there is an else block, that becomes the body of the _ => {} arm, otherwise _ => {} is provided...
RFC 160
RFC
2014-08-26
~4 min read
rust-lang.github.io
...Foo(x)
• The left side of a match arm, i.e. Foo(x) | Bar(x) if x > 5
• An entire match arm, i.e. Foo(x) | Bar(x) if x > 5 => 1
This RFC proposes only the first of these, but the others would be more useful in some cases...
RFC 85
RFC
2014-05-21
~1 min read
rust-lang.github.io
...if let A(x) = expr {
do_stuff_with(x);
} else if let B(x) = expr {
do_stuff_with(x);
}
or, using match:
match expr {
A(x) | B(x) => do_stuff_with(x),
_ => {},
}
This way of using match is seen multiple times in std::iter when dealing with the Chain iterator...
RFC 2175
RFC
2017-10-16
~7 min read
rust-lang.github.io
Summary
Permit matching sub-slices and sub-arrays with the syntax ...Binding a variable to the expression matched by a subslice pattern can be done using syntax <IDENT> @ .. similar to the existing <IDENT> @ <PAT> syntax, for example:
// Binding a sub-array:
let [x, y @ .., z] = [1, 2, 3, 4]; // `y...
RFC 2359
RFC
2018-03-08
~8 min read
rust-lang.github.io
...Lastly, there are alternatives that don't seem very favorable, but are listed for completeness sake:
• Remove unsafe from the API by returning a special SubSlice<'a> type instead of (uint, uint) in each match, that wraps the haystack and the current match as a (*start, *match_start, *match_end...
RFC 528
RFC
2015-02-17
~15 min read
rust-lang.github.io
...the existing .contains() method is incompatible with Needle API)
• .find(), .rfind(), .find_range(), .rfind_range()
• .matches(), .matches_mut(), .rmatches(), .rmatches_mut()
• .match_indices(), .match_indices_mut(), .rmatch_indices(), .rmatch_indices_mut()
• .match_ranges(), .match_ranges_mut(), .rmatch_ranges(), .rmatch_ranges_mut()
• .trim_matches(), .trim_start_matches(), .trim_end_matches()
• .replace...
RFC 2500
RFC
2018-07-06
~40 min read
rust-lang.github.io
Summary
Rust currently forbids pattern guards on match arms with move-bound variables. Allowing them would increase the applicability of pattern guards.
Motivation
Currently, if you attempt to use guards on a match arm with a move-bound variable, e.g.
struct A { a: Box<int> }
fn foo(n: int...
RFC 107
RFC
2014-06-05
~3 min read
rust-lang.github.io
...On floats are raw pointers, pattern matching behaves like ==, which means in particular that the value -0.0 matches the pattern 0.0, and NaN values match no pattern (except for wildcards).
Breaking changes
This RFC breaks code that compiles today, but only code that already emits a future compatibility...
RFC 3535
RFC
2023-11-19
~17 min read
rust-lang.github.io
...maybe_label FOR top_pat IN expr_nostruct block ;
For match expressions we now have:
expr_match : MATCH expr_nostruct '{' match_clause* nonblock_match_clause? '}' ;
match_clause : nonblock_match_clause ',' | block_match_clause ','? ;
nonblock_match_clause : match_arm (nonblock_expr | block_expr_dot) ;
block_match_clause : match_arm (block | block...
RFC 2535
RFC
2018-08-29
~18 min read
rust-lang.github.io
...An individual #[cfg(...)] attribute "matches" if all of the contained cfg patterns match the compilation environment, and an item preserved if it either has no #[cfg(...)] attributes or any of the #[cfg(...)] attributes present match.
This is problematic for several reasons:
• It is excessively verbose in certain situations. For example...
RFC 194
RFC
2014-08-09
~3 min read