doc.rust-lang.org
...The type of the body is `!` which matches the return type.
}
fn not_diverging() -> ! {
// This type is uninhabited.
// However, the entire function is not considered diverging.
make_empty();
// ERROR: The type of the body is `()` but expected type `!`.
}
[!NOTE] Divergence can propagate to the surrounding block. See [expr.block.diverging...
The Rust Reference
Book
2024-01-01
~1 min read
doc.rust-lang.org
...For example, match is a keyword. If you try to compile the following function that uses match as its name:
Filename: src/main.rs
fn match(needle: &str, haystack: &str) -> bool {
haystack.contains(needle)
}
you’ll get this error:
error: expected identifier, found keyword `match`
--> src/main.rs:4:4...
The Rust Programming Language
Book
2024-02-01
~3 min read
blog.cloudflare.com
...Understanding wildcards
Wildcard pattern matching allows users to employ an asterisk (*) in a string to match certain patterns. For example, a single pattern like https://example.com/*/t*st can cover multiple URLs such as https://example.com/en/test, https://example.com/images/toast, and https://example.com/blog...
Project/Tooling Updates
2024-08-28
~7 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
blog.yoshuawuyts.com
introduction
logical and
logical or
if let-evaluation ordering
closing words
Introduction
One of the things that stands out to me is how similar match and if..else
are semantically, while being diverging a fair bit syntactically. The reasons for that seem mostly accidental, and I have a sneaking suspicion...
Observations/Thoughts
2025-04-30
~10 min read
www.sminez.dev
...x/re/ for each match of re, run the expressions that follow
y/re/ between each match of re, run the expressions that follow
g/re/ if re matches, run the expressions that follow
v/re/ if re does not match, run the expressions that follow
For actions, the argument...
Observations/Thoughts
2025-11-19
~16 min read
ncameron.org
...As in a match expression, => indicates matching a pattern (in this case ()) to a body (in this case println!(...);). Since our use of the macro matches the single pattern (hello!() has no arguments), we expand into the given body.
The next example has some arguments, but still a single rule...
News & Blog Posts
2015-11-09
~4 min read
estada.ch
...Argument dependant lookup in match
unwrap_all()
Coupling split() and join()
Backwards compatibility
Argument dependant lookup in match
Consider this code for talking to a secondary processor:
#[derive(Debug)]
pub enum SecondaryProcessor {
Opening(String),
Ready,
Writing(String),
Reading(String),
Closing,
}
When we match on it today, we have to do...
Call for Blog Posts
2020-09-23
~4 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
doc.rust-lang.org
...match PrintOnDrop("Matched value in final expression") {
// Non-pattern-matching guards' temporaries are dropped once the
// condition has been evaluated
_ if PrintOnDrop("guard condition").0 == "" => (),
// Pattern-matching guards' temporaries are dropped when leaving the
// arm's scope
_ if let "guard scrutinee" = PrintOnDrop("guard scrutinee").0 => {
let _ = &PrintOnDrop("lifetime-extended temporary...
The Rust Reference
Book
2024-01-01
~16 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
dannas.name
...fn match_here(regex: &[u8], text: &[u8]) -> bool {
match (regex, text) {
(&[], _) => true,
(&[b'$'], &[]) => true,
(&[b'$'], _) => false,
(&[r, b'*', ref rs @ ..], txt) => {
match_star(r, rs, txt)
}
(&[_, ref _s @ ..], &[]) => false,
(&[r, ref rs @ ..], &[t, ref ts @ ..]) if r == b'.' || r == t => {
match_here(rs, ts)
}
_ => false,
}
}
Finally we have the matching...
Rust Walkthroughs
2022-09-14
~7 min read
www.gustavwengel.dk
...MyFood = serde_json::from_str(&json).unwrap();
// Does not work because no enum variant matches "tacos"
// Error returned is: Error("data did not match any variant of untagged enum MyFood")c
let json = json!(
{
"fruit_count": 5,
"foo": 3
}
)
.to_string();
let my_food: MyFood = serde_json::from_str(&json...
Rust Walkthroughs
2023-06-28
~3 min read
siciarz.net
...fn run(matches: ArgMatches) -> Result<(), String> {
// ...
match matches.subcommand() {
("analyse", Some(m)) => run_analyse(m, &logger),
("verify", Some(m)) => run_verify(m, &logger),
_ => Ok(()),
}
}
fn run_analyse(matches: &ArgMatches, parent_logger: &slog::Logger) -> Result<(), String> {
let logger = parent_logger.new(o!("command" => "analyse"));
let input = matches.value_of("input-file...
24 Days of Rust
2016-12-20
~6 min read
matklad.github.io
...char) -> ((), u8) {
match op {
'+' | '-' => ((), 9),
_ => panic!("bad op: {:?}", op),
}
}
fn postfix_binding_power(op: char) -> Option<(u8, ())> {
let res = match op {
'!' => (11, ()),
'[' => (11, ()),
_ => return None,
};
Some(res)
}
fn infix_binding_power(op: char) -> Option<(u8, u8)> {
let res = match op {
'=' => (2, 1),
'?' => (4, 3),
'+' | '-' => (5, 6),
'*' | '/' => (7, 8),
'.' => (14, 13...
News & Blog Posts
2020-04-21
~18 min read
matklad.github.io
...char) -> ((), u8) {
match op {
'+' | '-' => ((), 9),
_ => panic!("bad op: {:?}", op),
}
}
fn postfix_binding_power(op: char) -> Option<(u8, ())> {
let res = match op {
'!' => (11, ()),
'[' => (11, ()),
_ => return None,
};
Some(res)
}
fn infix_binding_power(op: char) -> Option<(u8, u8)> {
let res = match op {
'=' => (2, 1),
'?' => (4, 3),
'+' | '-' => (5, 6),
'*' | '/' => (7, 8),
'.' => (14, 13...
News & Blog Posts
2020-05-05
~18 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
doc.rust-lang.org
Enums and Pattern Matching
In this chapter, we’ll look at enumerations, also referred to as enums. Enums allow you to define a type by enumerating its possible variants. First we’ll define and use an enum to show how an enum can encode meaning along with data. Next, we...
The Rust Programming Language
Book
2024-02-01
~1 min read