blog.frankel.ch
...The language models it as an enum with generics on each value:
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub enum Result<T, E> {
Ok(T),
Err(E),
}
Because Rust manages completeness of matches, matching on Result enforces that you handle both branches:
match fn_that_returns_a_result...
Observations/Thoughts
2024-02-14
~4 min read
www.ntietz.com
...Vec<String>) -> Result<Matches, ParseError> {
let mut args_iter = args.into_iter();
let exec_name = match args_iter.next() {
Some(s) => s,
None => return Err(ParseError::MissingProgramName),
};
Next we setup our storage and populate the defaults with our helper function.
// pub fn parse(&self, args: Vec<String>) -> Result<Matches, ParseError...
Rust Walkthroughs
2024-11-06
~11 min read
ntietz.com
...Vec<String>) -> Result<Matches, ParseError> {
let mut args_iter = args.into_iter();
let exec_name = match args_iter.next() {
Some(s) => s,
None => return Err(ParseError::MissingProgramName),
};
Next we setup our storage and populate the defaults with our helper function.
// pub fn parse(&self, args: Vec<String>) -> Result<Matches, ParseError...
Rust Walkthroughs
2024-11-13
~11 min read
rodrigodd.github.io
...loop {
use Instruction::*;
match self.instructions[self.program_counter] {
// ...
Clear => self.memory[self.pointer] = 0,
}
// ...
}
And every time we parse a ] we check if the previous instructions match the
“clear cell” operation, and replace it by the new instruction.
b']' => {
let curr_address = instructions.len();
match bracket_stack.pop() {
Some...
Observations/Thoughts
2022-10-26
~20 min read
doc.rust-lang.org
...let mut s = String::new();
match process.stdout.unwrap().read_to_string(&mut s) {
Err(why) => panic!("couldn't read wc stdout: {}", why),
Ok(_) => print!("wc responded with:\n{}", s),
}
}
Rust by Example
Book
2024-01-01
~1 min read
blog.sheerluck.dev
...Vec<(usize, String)> = Vec::new();
let mut line_number = 1;
for line in contents.lines() {
let is_match = regex.is_match(line);
let should_include = if invert { !is_match } else { is_match };
if should_include {
results.push((line_number, line.to_string()));
}
line_number += 1;
}
results
}
We are borrowing contents...
Rust Walkthroughs
2026-04-15
~15 min read
siciarz.net
...Time for our first request!
extern crate hyper;
use hyper::Url;
use hyper::client::Request;
fn main() {
let url = match Url::parse("http://httpbin.org/status/200") {
Ok(url) => url,
Err(_) => panic!("Uh oh."),
};
println!("> get: {}", url);
let fresh_request = match Request::get(url) {
Ok(request) => request,
Err(_) => panic!("Whoops...
Blog Posts
2014-12-08
~4 min read
arzg.github.io
...u8) {
let checkpoint = p.checkpoint();
match p.peek() {
Some(SyntaxKind::Number) | Some(SyntaxKind::Ident) => p.bump(),
Some(SyntaxKind::Minus) => {
let op = PrefixOp::Neg;
let ((), right_binding_power) = op.binding_power();
}
_ => {}
}
loop {
let op = match p.peek() {
Some(SyntaxKind::Plus) => InfixOp::Add,
Some(SyntaxKind::Minus) => InfixOp::Sub,
Some(SyntaxKind::Star) => InfixOp...
Rust Walkthroughs
2020-11-18
~6 min read
rust-lang.github.io
...Finally, it is useful for the reader to keep in mind that according to the definitions of this RFC, no simple NT matches the empty fragment, and likewise no token matches the empty fragment of Rust syntax. (Thus, the only NT that can match the empty fragment is a complex...
RFC 550
RFC
2014-12-21
~21 min read
llogiq.github.io
...if the closing delimiter is on the same line, ignore both
if there is only one opening delimiter on that line, the set only contains
the horizontal position of the first non-whitespace character of that line
otherwise we need to match the position of the first non-whitespace character...
News & Blog Posts
2016-08-30
~2 min read
willspeak.me
...loop {
let mut state = Start;
let mut end = start;
for c in source[start..].chars() {
let next = match state {
Start => match c {
'(' => Some(Lparen),
')' => Some(Rparen),
'0'...'9' => Some(Number),
'a'...'z' => Some(Symbol),
c if c.is_whitespace() => Some(Whitespace),
_ => None,
},
Lparen | Rparen => None,
Number => match c {
'0'...'9...
News & Blog Posts
2019-07-16
~10 min read
blog.sheerluck.dev
...match
match evaluates one value, called the scrutinee and compares it against a set of patterns
fn main() {
let number = 2;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Something else"),
}
}
Each of these lines or sections inside the match block is called an arm, each...
Rust Walkthroughs
2026-09-09
~19 min read
doc.rust-lang.org
...Values that are matched with wildcards must still be initialized.
let x: u8;
let c = || {
let _ = x; // ERROR: Binding `x` isn't initialized.
};
Capturing for discriminant reads
If pattern matching reads a discriminant, the place containing that discriminant is captured by ImmBorrow.
Matching against a variant of an enum that...
The Rust Reference
Book
2024-01-01
~20 min read
rust-lang.github.io
Summary
Add a lifetime specifier for macro_rules! patterns, that matches any valid lifetime.
Motivation
Certain classes of macros are completely impossible without the ability to pass lifetimes. Specifically, anything that wants to implement a trait from inside of a macro is going to need to deal with lifetimes eventually...
RFC 1590
RFC
2016-04-22
~1 min read
audunhalland.github.io
...This meant that it wasn't possible to use the matching!-macro on that parameter.
The matching macro operates on immutable views of the function inputs, where a single, common lifetime parameter cuts it (because matching only reads things and does not return anything).
Given these limitations it became clear...
Project/Tooling Updates
2024-03-27
~3 min read
radekmie.dev
...At its core, the Matcher class does the heavy lifting and handles all the querying logic, i.e., checking whether a document matches a query.In JavaScript, it’s implemented as a cascade of nested closures, i.e., functions take some information about the query and return a function that...
Observations/Thoughts
2025-07-23
~7 min read
andreabergia.com
...Just as it should be. 😊
Finding the exception handler
Link to heading
Finding a matching exception handler is just a question of checking all of them, in order, and stopping with the first one that matches, i.e. whose catch class is a superclass of the actual exception class, since...
Miscellaneous
2023-09-27
~9 min read
nnethercote.github.io
...impl ::core::cmp::Ord for Point {
#[inline]
fn cmp(&self, other: &Point) -> ::core::cmp::Ordering {
- match *other {
- Self {
- x: ref __self_1_0,
- y: ref __self_1_1,
- } => match *self {
- Self {
- x: ref __self_0_0,
- y: ref __self_0_1,
- } => match ::core::cmp::Ord::cmp(&(*__self_0_0), &(*__self...
Observations/Thoughts
2022-07-20
~16 min read
rust-lang.github.io
Summary
Add a literal fragment specifier for macro_rules! patterns that matches literal constants:
macro_rules! foo {
($l:literal) => ( /* ... */ );
};
Motivation
There are a lot of macros out there that take literal constants as arguments (often string constants). For now, most use the expr fragment specifier, which is fine since literal...
RFC 1576
RFC
2016-04-08
~2 min read
rust-lang.github.io
...Optional match sugar
Originally, the RFC included the ability to match the errors caught by a catch by writing catch { .. } match { .. }, which could be translated as follows:
•
Construct:
catch {
foo()?.bar()
} match {
A(a) => baz(a),
B(b) => quux(b)
}
Shallow:
match (catch {
foo()?.bar()
}) {
Ok(a) => a,
Err(e...
RFC 243
RFC
2014-09-16
~20 min read