github.com
...To reduce risk of tools in the ecosystem unintentionally matching pre-releases (despite them still needing an opt-in),
it might be reasonable for the
`semver`
package to offer this new matching behavior under a different name
(e.g. `VersionReq::matches_prerelease` in contrast to the existing `VersionReq::matches`)
(also...
RFC 3493
RFC
2023-09-20
~9 min read
softprops.github.io
...Option<u64> } fn main() { match envy::from_env::<Config>() { Ok(config) => println!("{:#?}", config), Err(error) => panic!("{:#?}", error) } } ... export some environment variables $ FOO=8080 BAR=true BAZ=hello yourapp You should be able to access a completely typesafe config struct deserialized from env vars. Envy assumes an env var exists for...
Crate
v0.4.2
2021-01-04
rust.code-maven.com
...u32 = match args[1].parse() {
Ok(value) => value,
Err(err) => {
eprintln!("Invalid parameter: '{}'. It must be an integer", err);
eprintln!("Usage: {} INTEGER", &args[0]);
std::process::exit(1);
}
};
number
}
Here we have a match that is expected to return a 32-bit unsigned integer, a u32.
However, if the user...
Miscellaneous
2023-12-06
~3 min read
boats.gitlab.io
...Match ergonomics
Match ergonomics have been stable for a while, but I continue to find them
paying dividends. I’m sure there are times where I’ve used it without even
noticing it, but there were many cases where I did notice. My experience
usually went like this:
I write...
News & Blog Posts
2018-07-31
~6 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
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
kerkour.com
...Pattern matching in Rust can be used to match against many other expressions:
match x {
42 => println!("Good!"),
_ => println!("Bad!"),
}
let boolean = true;
// Match is an expression too
let binary = match boolean {
false => 0,
true => 1,
};
let x = Some(42u64);
match x {
Some(1) => println!("1"),
Some(42) => println!("42...
Rust Walkthroughs
2022-03-09
~9 min read
crates.io
...a test my_test that accepts a path, and optionally the contents as input a directory to look for files (test fixtures) in a pattern to match files on datatest-stable will call the my_test function once per matching file in the directory. Directory traversals are recursive. datatest-stable...
Crate
v0.3.3
2026-03-31
smallcultfollowing.com
...it starts as we enter the match
(match &message) and continues into the match arm. On the else
branch of the match, the borrow is still in use (in the form of the
data variable), but in the if branch, it is not (and hence we can
call tx.send...
News & Blog Posts
2018-11-06
~5 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
www.diegofreijo.com
...pub fn scan_token(&mut self) -> TokenResult<'a> {
self.skip_whitespaces();
self.start = self.current;
match self.advance() {
Some(c) => match c {
_ if Scanner::is_alpha(c) => self.identifier(),
_ if Scanner::is_digit(c) => self.number(),
// Single-char tokens
'(' => self.make_token(TokenType::LeftParen),
')' => self.make_token(TokenType::RightParen),
// (...)
'<' => self...
Rust Walkthroughs
2022-02-09
~13 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.1
2026-08-03
blog.burntsushi.net
...Match
In this case, Split means “jump to two different instructions
simultaneously.” In particular, if either branch executes the Match
instruction, then the entire regex will match. If both Char instructions
fail, then it’s impossible to reach the Match instruction.
Clarifying the divide: native regexes vs. dynamic regexes
The...
Community Updates
2014-05-05
~18 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
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
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
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