kerkour.com
...Then we can handle a potential error with match.
fn question() -> Result<(), Error> {
let x = // ...
match ultimate_answer(x) {
Ok(_) => // do something
Err(Error::More) => // do something
Err(Error::Less) => // do something
Err(Error::WrongAnswer) => // do something
}
// ...
}
Or, the most common way to handle errors, forward them with ?.
fn question...
Observations/Thoughts
2022-02-09
~4 min read
crates.io
...When the specified expectations do not match the received request, mock.assert() fails the test with a detailed error description, including a diff that shows the differences between the expected and actual HTTP requests. Example: 0 of 1 expected requests matched the mock specification. Here is a comparison with the...
Crate
v0.8.3
2026-02-04
sireliah.com
...6 nodes.sort_by(|a, b| b.prob.partial_cmp(&a.prob;).unwrap()); 7 8 let first = match nodes.pop() { 9 Some(i) => i,10 None => return,11 };12 13 let second: Node = match nodes.pop() { 14 Some(i) => i,15 None => return,16 };17 18 /// Create new node and...
News & Blog Posts
2017-06-27
~8 min read
www.bekk.christmas
...Then we pattern match on the vector as a slice – a view into an array.When the compiler sees a match expression, it matches the value with the list of patterns and stops when encountering the first pattern that matches. Each branch has the form of SomePattern => some_expression(),. For...
Rust Walkthroughs
2022-12-07
~17 min read
bertptrs.nl
...my_print!(match) would not be accepted.
Edition 2024 brings the :tt fragment specifier up-to-date with the new additions to the language,
allowing it to match _- and const expressions as part of an :expr fragment. If you must
maintain the previous behaviour, you can replace :expr with :expr...
Observations/Thoughts
2025-02-26
~17 min read
blog.logrocket.com
...Declarative macros provide a match like an interface where on match the macro is replaced with code inside the matched arm.
Creating declarative macros
// use macro_rules! <name of macro>{<Body>}
macro_rules! add{
// macth like arm for macro
($a:expr,$b:expr)=>{
// macro expand to this code
{
// $a and...
Rust Walkthroughs
2021-02-03
~17 min read
doc.rust-lang.org
pub fn offset(&self) -> usize
...This means that, when the iterator has not been fully consumed, the returned value will match the index that will be returned by the next call to next().
Examples
let mut chars = "a楽".char_indices();
// `next()` has not been called yet, so `offset()` returns the byte
// index of the first...
method
core
Stable since 1.82.0
Version 1.100.0-nightly
www.sea-ql.org
...513] Added the MATCH, -> and ->> operators for SQLite
use sea_query::extension::sqlite::SqliteBinOper;assert_eq!( Query::select() .column(Char::Character) .from(Char::Table) .and_where(Expr::col(Char::Character).binary(SqliteBinOper::Match, Expr::val("test"))) .build(SqliteQueryBuilder), ( r#"SELECT "character" FROM "character" WHERE "character" MATCH ?"#.to_owned(), Values(vec...
Project/Tooling Updates
2023-01-04
~3 min read
docs.rs
...If the output matches, the test passes, and if the output doesn't match the test fails. Note that it is also considered a failure if a test actually compiles successfully. To add a test: Create ui-tests/my-awesome-test.rs Write an invalid #[wasm_bindgen] invocation, testing the...
Crate
v0.2.127
2026-08-08
doc.rust-lang.org
primitive bool
...or, a match pattern
match praise_the_borrow_checker {
true => println!("keep praising!"),
false => println!("you should praise!"),
}
Also, since bool implements the Copy trait, we don't have to worry about the move semantics (just like the integer and float primitives).
Now an example of bool cast to integer...
primitive
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
primitive bool
...or, a match pattern
match praise_the_borrow_checker {
true => println!("keep praising!"),
false => println!("you should praise!"),
}
Also, since bool implements the Copy trait, we don't have to worry about the move semantics (just like the integer and float primitives).
Now an example of bool cast to integer...
primitive
std
Stable since 1.0.0
Version 1.100.0-nightly
iev.ee
...i also added a \p{utf8} class that lets you constrain matches to valid UTF-8. rust's regex crate guarantees that matches only occur on valid UTF-8 boundaries, so how do you do that when your engine operates on raw bytes? you intersect (&) with the language of valid...
Observations/Thoughts
2026-03-11
~12 min read
doc.rust-lang.org
pub fn chars(&self) -> Chars<'_>
...It's important to remember that char represents a Unicode Scalar Value, and might not match your idea of what a 'character' is. Iteration over grapheme clusters may be what you actually want. This functionality is not provided by Rust's standard library, check crates.io instead.
Examples
Basic usage...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
crates.io
...ExampleError) -> Self { solana_program_error::ProgramError::Custom(e as u32) } } impl solana_program_error::ToStr for ExampleError { fn to_str<E>(&self) -> &'static str { match self { ExampleError::MintHasNoMintAuthority => "Mint has no mint authority", ExampleError::IncorrectMintAuthority => "Incorrect mint authority has signed the instruction", } } }
Crate
v0.8.0
2025-08-11
blog.burntsushi.net
...option-ex-string-findfn main_find() {
let file_name = "foobar.rs";
match find(file_name, '.') {
None => println!("No file extension found."),
Some(i) => println!("File extension: {}", &file_name[i+1..]),
}
}
This code uses pattern
matching to do case
analysis on the Option<usize> returned by the find function. In fact...
Notable Links
2015-05-18
~57 min read
wapl.es
...This needs to
be wrapped in a match to be usable by sort_unstable_by in Triangle::sorted_clockwise:
points.sort_unstable_by(|a, b| {
let order = sort_clockwise(*a, *b, center);
match order {
true => Ordering::Greater,
false => Ordering::Less
}
});
Changing to use Ordering
The above code doesn't feel...
Miscellaneous
2020-07-28
~11 min read
getcode.substack.com
...Let's try evaluating them by writing an interpreter.impl Expr {
fn eval(&self) -> i32 {
match self {
Expr::Lit(i) => *i,
Expr::Neg(r) => -r.eval(),
Expr::Add(r1, r2) => r1.eval() + r2.eval(),
}
}
}Easy enough - we pattern match on Expr and recursively call eval. Calling eval on the example...
Rust Walkthroughs
2023-03-29
~25 min read
crates.io
...Structured like an if-else chain, the first matching branch is the
item that gets emitted.
cfg-if Documentation A macro to ergonomically define an item depending on a large number of #[cfg] parameters. Structured like an if-else chain, the first matching branch is the item that gets emitted...
Crate
v1.0.4
2025-10-15
immunant.com
...We added some extra sanity checking for global arrays and found real-world examples where the size of an array declaration didn’t match its definition. Aside from that case, it seems that in practice most global declaration types match their definitions. Functions are another matter entirely. We match extern...
News & Blog Posts
2019-12-24
~15 min read
xion.io
...let result = results.into_iter().fold(Ok(vec![]), |mut v, r| match r {
Ok(x) => { v.as_mut().map(|v| v.push(x)); v },
Err(e) => Err(e),
});
and in a loop form:
let mut result = Ok(vec![]);
for r in results {
match r {
Ok(x) => result.as_mut().map...
News & Blog Posts
2017-04-11
~5 min read