crates.io
...String, } } // disabled variant is matched let config = toml::from_str::<Config>(r#" [feature] enabled = false "#).unwrap(); assert!(matches!(config.feature, FeatureConfig::Disabled { .. })); // if the type used `enabled: bool`, this would cause issues and require Option<_> wrappers plus // further validation... instead an error is returned immediately regarding the missing fields let...
Crate
v0.1.6
2026-10-05
rustc-dev-guide.rust-lang.org
...If there are multiple matching candidates in a group, we report an error, except that multiple impls of the same trait are treated as a single match. Otherwise we pick the first match we find.
In the case of our example, the first step is Rc<Box<[T; 3]>>, which...
Guide to Rustc Development
Book
2024-01-01
~3 min read
doc.rust-lang.org
...Option<Food>) -> Option<Peeled> {
match food {
Some(food) => Some(Peeled(food)),
None => None,
}
}
// Chopping food. If there isn't any, then return `None`.
// Otherwise, return the chopped food.
fn chop(peeled: Option<Peeled>) -> Option<Chopped> {
match peeled {
Some(Peeled(food)) => Some(Chopped(food)),
None => None,
}
}
// Cooking food. Here, we showcase...
Rust by Example
Book
2024-01-01
~1 min read
vojtechkral.github.io
...fn exprs() {
fn make_token(s: &'static str) -> Result<Token, ()> {
Ok(Token(s))
}
match make_token("matched token") {
Ok(_) => println!("match arm"),
Err(_) => unreachable!(),
}
println!("after match");
if let Ok(_) = make_token("if let token") {
println!("if let body");
}
println!("after if let");
if make_token("if token").is_ok...
Observations/Thoughts
2020-09-30
~7 min read
fast.github.io
...fn parse_value_iterative(tokens: &mut TokenStream) -> JsonValue { let mut stack = vec![ParseState::ParseValue]; let mut results = Vec::new(); while let Some(state) = stack.pop() { match state { ParseState::ParseValue => { match tokens.peek() { Token::LeftBrace => { stack.push(ParseState::ParseObject(HashMap::new())); } Token::LeftBracket => { stack.push(ParseState::ParseArray(Vec::new())); } _ => { results.push...
Project/Tooling Updates
2025-07-23
~4 min read
doc.rust-lang.org
...use std::num::ParseIntError;
fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
let first_number = match first_number_str.parse::<i32>() {
Ok(first_number) => first_number,
Err(e) => return Err(e),
};
let second_number = match second_number_str.parse::<i32>() {
Ok(second_number) => second_number...
Rust by Example
Book
2024-01-01
~1 min read
docs.rs
...end() Match end of input. any() Match any symbol and return the symbol. sym(t) Match a single terminal symbol t . seq(s) Match sequence of symbols. list(p,s) Match list of p , separated by s . one_of(set) Success when current input symbol is one of the set...
Crate
v3.4.0
2024-03-06
doc.rust-lang.org
...Finally, it is useful for the reader to keep in mind that according to the definitions of this formalism, 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...
The Rust Reference
Book
2024-01-01
~13 min read
anoopelias.github.io
...Sample code below,macro_rules! assert_match {
($exp:expr, $pattern1:pat_param | $pattern2:pat) => {
match $exp {
$pattern1 => {
println!("Pattern 1 match");
}
$pattern2 => {
println!("Pattern 2 match");
}
_ => panic!("Failed match"),
}
}
}
fn main() {
let value = 2;
assert_match!(value, 1 | 2);
}
metaMeta specifier is used to send #[xxx] type attributes to the...
Rust Walkthroughs
2024-02-07
~8 min read
doc.rust-lang.org
...Since pattern matching is like reading the union with a particular field, it has to be placed in unsafe blocks as well.
# union MyUnion { f1: u32, f2: f32 }
#
fn f(u: MyUnion) {
unsafe {
match u {
MyUnion { f1: 10 } => { println!("ten"); }
MyUnion { f2 } => { println!("{}", f2); }
}
}
}
Pattern matching may match a union...
The Rust Reference
Book
2024-01-01
~4 min read
docs.rs
...Each bit in the bitmap indicates a bit matching pattern: bit 0 1 2 3 4 5 6 7 match * 0* 1* 00* 01* 10* 11* 000* bit 8 9 10 11 12 13 14 15 match 001* 010* 011* 100* 101* 110* 111* endnode-bit The last bit here...
Crate
v0.5.0
2020-03-30
doc.rust-lang.org
...The exception is that the outer delimiters for the matcher will match any pair of delimiters. Thus, for instance, the matcher (()) will match {()} but not {{}}. The character $ cannot be matched or transcribed literally.
Forwarding a matched fragment
When forwarding a matched fragment to another macro-by-example, matchers in the...
The Rust Reference
Book
2024-01-01
~18 min read
huonw.github.io
Rust’s match statement can do a lot of things, even C-style fallthough to the next branch, despite having no real support for it. It turns out to be a “shallow” feature, where the C to Rust translation is easily done, without needing to understand the code itself. The...
Observations/Thoughts
2025-03-05
~8 min read
stopa.io
...fn env_get(k: &str, env: &RispEnv) -> Option<RispExp> {
match env.data.get(k) {
Some(exp) => Some(exp.clone()),
None => {
match &env.outer {
Some(outer_env) => env_get(k, &outer_env),
None => None
}
}
}
}
fn eval(exp: &RispExp, env: &mut RispEnv) -> Result<RispExp, RispErr> {
match exp {
RispExp::Symbol(k) =>
env_get...
Rust Walkthroughs
2020-12-02
~15 min read
doc.rust-lang.org
?
Chaining results using match can get pretty untidy; luckily, the ? operator can be used to make things pretty again. ? is used at the end of an expression returning a Result, and is equivalent to a match expression, where the Err(err) branch expands to an early return Err(From::from...
Rust by Example
Book
2024-01-01
~1 min read
blog.meilisearch.com
...IS EMPTY matches existing attributes with empty value while IS NULL matches fields with a null value.
Here’s an example considering the following documents:
[
{
"id": 0,
"color": []
},
{
"id": 1,
"color": null
},
{
"id": 2,
}
]
The new filter operators work like this:
color IS EMPTY: matches document 0
color IS NULL...
Project/Tooling Updates
2023-06-07
~4 min read
doc.rust-lang.org
pub fn contains<P>(&self, pat: P) -> bool
str::contains — Returns true if the given pattern matches a sub-slice of this string slice.
Returns false if it does not.
The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.
Examples
let bananas = "bananas";
assert!(bananas...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
docs.rs
...import re re.compile('(a|b|ab)*bc').match('ab' * 28 + 'ac') In Python (tested on both 2.7 and 3.5), this match takes 91s, and doubles for each additional repeat of 'ab'. Thus, many proponents advocate a purely NFA (nondeterministic finite automaton) based approach. Even so, backreferences and...
Crate
v0.20.2
2026-11-11
dev.to
...fn read_username_from_file() -> Result<String, io::Error> {
let f = File::open("username.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e),
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
}
Enter fullscreen mode...
Learn Simple Rust
2020-10-21
~4 min read
leshow.github.io
...Write,
{
match var {
Var::One => serde_json::to_writer(&mut writer, &Foo),
Var::Two => serde_json::to_writer(&mut writer, &Bar),
}
}
fn write_pretty<W>(var: Var, mut writer: W) -> serde_json::Result<()>
where
W: Write,
{
match var {
Var::One => serde_json::to_writer_pretty(&mut writer, &Foo),
Var::Two...
News & Blog Posts
2020-05-05
~5 min read