rust-lang.github.io
Summary
Change pattern matching on an &mut T to &mut <pat>, away from its current &<pat> syntax.
Motivation
Pattern matching mirrors construction for almost all types, except &mut, which is constructed with &mut <expr> but destructured with &<pat>. This is almost certainly an unnecessary inconsistency.
This can and does lead...
RFC 179
RFC
2015-01-04
~2 min read
rust-lang.github.io
...This is a counterpart to if let expressions, and the pattern matching works identically, except that the value from the pattern match is assigned to the surrounding scope rather than the block's scope.
Reference-level explanations
let-else is syntactical sugar for match where the non-matched case diverges...
RFC 3137
RFC
2021-05-31
~20 min read
rust-lang.github.io
...Specifically, the syntax
['ident:] while let PAT = EXPR {
BODY
}
desugars to
['ident:] loop {
match EXPR {
PAT => BODY,
_ => break
}
}
Just as with if let, an irrefutable pattern given to while let is considered an error. This is largely an artifact of the fact that the desugared match ends up with an...
RFC 214
RFC
2014-08-27
~2 min read
github.com
...a `match` statement takes one expression and applies multiple patterns to it until one matches,
while let-else-else-chains would take one pattern and apply it to multiple expressions until one matches.
This has a complexity issue with or-patterns, where expressions can _easily_ become exponential.
(This is already...
RFC 3137
RFC
2021-05-31
~21 min read
rust-lang.github.io
Summary
Change syntax of subslices matching from ..xs to xs.. to be more consistent with the rest of the language and allow future backwards compatible improvements.
Small example:
match slice {
[xs.., _] => xs,
[] => fail!()
}
This is basically heavily stripped version of RFC 101.
Motivation
In Rust, symbol after .. token usually describes...
RFC 202
RFC
2014-08-15
~1 min read
rust-lang.github.io
Summary
Rust's support for pattern matching on slices has grown steadily and incrementally without a lot of oversight. We have concern that Rust is doing too much here, and that the complexity is not worth it. This RFC proposes to feature gate multiple-element slice matches in the head...
RFC 164
RFC
2014-07-14
~1 min read
rust-lang.github.io
...Adding this hint to enums will force downstream crates to add a wildcard arm to match statements, ensuring that adding new variants is not a breaking change.
Adding this hint to structs or enum variants will prevent downstream crates from constructing or exhaustively matching, to ensure that adding new fields...
RFC 2008
RFC
2017-05-24
~12 min read
github.com
...Adding this hint to enums will force downstream crates to add a wildcard arm to
`match` statements, ensuring that adding new variants is not a breaking change.
Adding this hint to structs or enum variants will prevent downstream crates
from constructing or exhaustively matching, to ensure that adding new fields...
RFC 2008
RFC
2017-05-24
~12 min read
rust-lang.github.io
...Pattern matching
Unsafe code may pattern match on union fields, using the same syntax as a struct, without the requirement to mention every field of the union in a match or use ..:
fn f(u: MyUnion) {
unsafe {
match u {
MyUnion { f1: 10 } => { println!("ten"); }
MyUnion { f2 } => { println!("{}", f2); }
}
}
}
Matching a...
RFC 1444
RFC
2015-12-29
~12 min read
rust-lang.github.io
...pub trait PartialOrd {
fn partial_cmp(&self, other: &Self) -> Option<Ordering>;
fn lt(&self, other: &Self) -> bool {
match self.partial_cmp(other) {
Some(Less) => true,
_ => false,
}
}
fn le(&self, other: &Self) -> bool {
match self.partial_cmp(other) {
Some(Less) | Some(Equal) => true,
_ => false,
}
}
fn gt(&self, other: &Self) -> bool {
match...
RFC 100
RFC
2014-06-01
~2 min read
rust-lang.github.io
...Matching
When an OsStr is used for matching, an unpaired low surrogate at the beginning and unpaired high surrogate at the end must be replaced by regular expressions that match all pre-canonicalization possibilities. For instance, matching for xxxx\u{d9ab} would create the following regex:
xxxx(
\xed\xa6\xab...
RFC 2295
RFC
2018-01-16
~13 min read
rust-lang.github.io
...Some libraries (like Python's re and RE2/C++) distinguish between matching an expression against an entire string and matching an expression against part of the string. My implementation favors simplicity: matching the entirety of a string requires using the ^ and/or $ anchors. In all cases, an implicit .*? is added...
RFC 42
RFC
2014-04-12
~9 min read
rust-lang.github.io
...i32 = match some_bool {
true => 23,
false => panic!("aaah!"), // an expression of type `!`, gets cast to `i32`
}
match break {
() => 23, // matching with a `()` forces the match argument to be cast to type `()`
}
These casts can be implemented by having the compiler assign a fresh, diverging type variable to any expression...
RFC 1216
RFC
2015-07-19
~12 min read
github.com
...i32 = match some_bool {
true => 23,
false => panic!("aaah!"), // an expression of type `!`, gets cast to `i32`
}
match break {
() => 23, // matching with a `()` forces the match argument to be cast to type `()`
}
```
These casts can be implemented by having the compiler assign a fresh, diverging
type variable to any expression...
RFC 1216
RFC
2015-07-19
~13 min read
rust-lang.github.io
...These fragment specifiers define what Rust syntax will be matched and bound to each metavariable. For example, the item fragment specifier matches an item, block matches a block expression, expr matches an expression, and so on.
As we add new features to Rust, sometimes we change its syntax. This means...
RFC 3531
RFC
2023-11-16
~7 min read
rust-lang.github.io
...target, which matches the entire target triple string (e.g. arm-unknown-linux-gnueabihf). This also adds a CARGO_CFG_TARGET environment variable for parity with other CARGO_CFG_* variables.
In addition, this proposes a shorthand cfg(target(...)) to match multiple components of a target string at once.
Motivation
To...
RFC 3239
RFC
2020-09-27
~2 min read
rust-lang.github.io
...In particular, at the time that mailing list thread was created, the code match match x {} ... would be parsed as match (x {}) ..., not as (match x {}) ... (see Rust PR 5137); likewise, if x {} would be parsed as an if-expression whose test component is the struct literal x {}. Thus, at...
RFC 218
RFC
2014-08-28
~12 min read
rust-lang.github.io
...match map.entry(key) => {
Entry::Vacant(entry) => { entry.insert(1); },
Entry::Occupied(entry) => { *entry.get_mut() += 1; },
}
match map.entry(key) => {
Entry::Vacant(entry) => { entry.insert(vec![val]); },
Entry::Occupied(entry) => { entry.get_mut().push(val); },
}
This code is noisy, and is visibly fighting the Entry API a bit, such...
RFC 921
RFC
2015-03-01
~2 min read
rust-lang.github.io
Summary
Enable if and match during const evaluation and make them evaluate lazily. In short, this will allow if x < y { y - x } else { x - y } even though the else branch would emit an overflow error for unsigned types if x < y.
Motivation
Conditions in constants are important for making...
RFC 2342
RFC
2018-01-11
~3 min read
rust-lang.github.io
...DefId) -> ParamEnv<'tcx> {
if tcx.describe_def(def_id) match Some(Def::Existential(_))
&& tcx.hir.as_local_node_id(def_id) match Some(node_id)
&& tcx.hir.get(node_id) match hir::map::NodeItem(item)
&& item.node match hir::ItemExistential(ref exist_ty)
&& exist_ty.impl_trait_fn match Some...
RFC 2497
RFC
2018-07-13
~54 min read