lwn.net
...Matches
involving tuples, for example, must try matching a single element at a
time, which is something that the GCC internal representation wasn't
designed to do. Arm guards (essentially an extra if controlling
whether a specific match occurs) also complicate things, since the
variables set by the match must...
Observations/Thoughts
2022-10-26
~8 min read
rust-analyzer.github.io
...ignore macro imports from extern crate self.
#8863 don’t add extra whitespace around fields.
#8880 fix module renaming.
#8875 avoid false positive "Missing match arm" when an or-pattern has mismatched types.
#8884 fix "Add explicit type" producing invalid code on @ patterns.
#8893 update outdated auto-import documentation.
#8902...
Project/Tooling Updates
2021-05-26
~1 min read
sentry.io
...use sentry_log::LogFilter; let logger = sentry_log::SentryLogger::new().filter(|md| match md.level() { log::Level::Error => LogFilter::Event, _ => LogFilter::Ignore, }); Sending multiple items to Sentry To map a log record to multiple items in Sentry, you can combine multiple log filters using the bitwise or operator: use sentry...
Crate
v0.49.6
2026-11-16
doc.rust-lang.org
...3 };
match foo {
Foo { x: (1, b), y } => println!("First of x is 1, b = {}, y = {} ", b, y),
// you can destructure structs and rename the variables,
// the order is not important
Foo { y: 2, x: i } => println!("y is 2, i = {:?}", i),
// and you can also ignore some variables:
Foo...
Rust by Example
Book
2024-01-01
~1 min read
santiagopastorino.com
...usize) -> &mut String {
match map.get_mut(&key) {
Some(value) => value,
None => {
map.insert(key, "".to_string());
map.get_mut(&key).unwrap()
}
}
}
fn main() {
let map = &mut HashMap::new();
map.insert(22, format!("Hello, world"));
map.insert(44, format!("Goodbye, world"));
assert_eq!(&*get_default(map, 22), "Hello, world");
assert...
News & Blog Posts
2018-01-09
~5 min read
doc.rust-lang.org
...let mut into_iter = vec2.into_iter();
// `iter()` yields `&i32`, and `find` passes `&Item` to the predicate.
// Since `Item = &i32`, the closure argument has type `&&i32`,
// which we pattern-match to dereference down to `i32`.
println!("Find 2 in vec1: {:?}", iter.find(|&&x| x == 2));
// `into_iter()` yields `i32`, and...
Rust by Example
Book
2024-01-01
~1 min read
crates.io
Generates random strings and byte strings matching a regex rand_regex Generates random strings and byte strings matching a regex. Examples use rand::{SeedableRng, Rng}; let mut rng = rand_xorshift::XorShiftRng::from_seed(*b"The initial seed"); // creates a generator for sampling strings let grx = rand_regex::Regex::compile(r...
Crate
v0.19.0
2026-02-24
doc.rust-lang.org
fn all<F>(&mut self, f: F) -> bool
Iterator::all — Tests if every element of the iterator matches a predicate.
all() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if they all return true, then so does all(). If any of them return false, it returns false...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
fn any<F>(&mut self, f: F) -> bool
Iterator::any — Tests if any element of the iterator matches a predicate.
any() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if any of them return true, then so does any(). If they all return false, it returns false...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
while let
Similar to if let, while let can make awkward match sequences more tolerable. Consider the following sequence that increments i:
// Make `optional` of type `Option<i32>`
let mut optional = Some(0);
// Repeatedly try this test.
loop {
match optional {
// If `optional` destructures, evaluate the block.
Some(i) => {
if i...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
...use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
fn main() {
// Create a path to the desired file
let path = Path::new("hello.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let mut file = match File::open(&path) {
Err...
Rust by Example
Book
2024-01-01
~1 min read
solang.readthedocs.io
...function get() public view returns (bool) { return value; } } "#, 0).unwrap(); for part in &tree.0 { match part { SourceUnitPart::ContractDefinition(def) => { println!("found contract {:?}", def.name); for part in &def.parts { match part { ContractPart::VariableDefinition(def) => { println!("variable {:?}", def.name); } ContractPart::FunctionDefinition(def) => { println!("function {:?}", def.name); } _ => (), } } } _ => (), } }
Crate
v0.3.5
2025-06-22
tonyarcieri.com
...Rust’s pattern matching lets us write match statements that work across all of the variants:
match animal {
Animal::Cat { weight, .. } |
Animal::Dog { weight, .. } |
Animal::Monkey { weight, .. } |
Animal::Fish { weight, .. } |
Animal::Dolphin { weight, .. } |
Animal::Snake { weight, .. } => weight
}
This provides a way to query an attribute across all of the variants...
News & Blog Posts
2016-11-15
~5 min read
xd009642.github.io
...use std::{
backtrace::{Backtrace, BacktraceStatus},
io::{self, Write},
panic::{self, PanicHookInfo},
thread,
};
fn default_like_hook(info: &PanicHookInfo<'_>) {
let thread = thread::current();
let name = thread.name().unwrap_or("<unnamed>");
let message = match info.payload().downcast_ref::<&str>() {
Some(message) => *message,
None => match info.payload().downcast_ref::<String>() {
Some(message) => message...
Observations/Thoughts
2026-11-11
~4 min read
doc.rust-lang.org
...fn main() {
// Try changing the values in the array, or make it a slice!
let array = [1, -2, 6];
match array {
// Binds the second and the third elements to the respective variables
[0, second, third] =>
println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third),
// Single values can be ignored...
Rust by Example
Book
2024-01-01
~1 min read
docs.rs
...Parsing use svg::node::element::path::{Command, Data}; use svg::node::element::tag::Path; use svg::parser::Event; let path = "image.svg"; let mut content = String::new(); for event in svg::open(path, &mut content).unwrap() { match event { Event::Tag(Path, _, attributes) => { let data = attributes.get("d").unwrap(); let data...
Crate
v0.18.0
2024-09-27
www.shuttle.rs
...The advantage of using an enum over just strings is that when we're pattern matching, we can simply match against the different variants instead of having to account for variations in strings.
Enums in Other Languages#
For some context, let's have a look at what enums look like...
Rust Walkthroughs
2023-11-29
~6 min read
crates.io
Glob-matched recursive file system walking. GlobWalk Recursively find files in a directory using globs. This crate is now in a perpetual maintnance mode and new users should probably cosider using glob . Comparison to the glob crate This crate was origially written years ago, when glob was a very differet...
Crate
v0.9.1
2024-01-06
www.abubalay.com
...let mut elements = elements_value(lex, value)?;
loop {
match array_open_elements(elements) {
Either::Left(e) => elements = e,
Either::Right(array) => return Ok(array),
}
}
}
// array = "[" elements * "]"
// elements = elements * "," value
fn array_open_elements(lex: &mut Lex, elements: Elements) ->
Result<Either<Elements, Array>, ParseError>
{
let token = lex.token();
match token {
Token...
News & Blog Posts
2018-04-10
~14 min read
doc.rust-lang.org
...RGB(u32, u32, u32),
HSV(u32, u32, u32),
HSL(u32, u32, u32),
CMY(u32, u32, u32),
CMYK(u32, u32, u32, u32),
}
fn main() {
let color = Color::RGB(122, 17, 40);
// TODO ^ Try different variants for `color`
println!("What color is it?");
// An `enum` can be destructured using a `match`.
match...
Rust by Example
Book
2024-01-01
~1 min read