www.gustavwengel.dk
...MyFood = serde_json::from_str(&json).unwrap();
// Does not work because no enum variant matches "tacos"
// Error returned is: Error("data did not match any variant of untagged enum MyFood")c
let json = json!(
{
"fruit_count": 5,
"foo": 3
}
)
.to_string();
let my_food: MyFood = serde_json::from_str(&json...
Rust Walkthroughs
2023-06-28
~3 min read
siciarz.net
...fn run(matches: ArgMatches) -> Result<(), String> {
// ...
match matches.subcommand() {
("analyse", Some(m)) => run_analyse(m, &logger),
("verify", Some(m)) => run_verify(m, &logger),
_ => Ok(()),
}
}
fn run_analyse(matches: &ArgMatches, parent_logger: &slog::Logger) -> Result<(), String> {
let logger = parent_logger.new(o!("command" => "analyse"));
let input = matches.value_of("input-file...
24 Days of Rust
2016-12-20
~6 min read
matklad.github.io
...char) -> ((), u8) {
match op {
'+' | '-' => ((), 9),
_ => panic!("bad op: {:?}", op),
}
}
fn postfix_binding_power(op: char) -> Option<(u8, ())> {
let res = match op {
'!' => (11, ()),
'[' => (11, ()),
_ => return None,
};
Some(res)
}
fn infix_binding_power(op: char) -> Option<(u8, u8)> {
let res = match op {
'=' => (2, 1),
'?' => (4, 3),
'+' | '-' => (5, 6),
'*' | '/' => (7, 8),
'.' => (14, 13...
News & Blog Posts
2020-04-21
~18 min read
matklad.github.io
...char) -> ((), u8) {
match op {
'+' | '-' => ((), 9),
_ => panic!("bad op: {:?}", op),
}
}
fn postfix_binding_power(op: char) -> Option<(u8, ())> {
let res = match op {
'!' => (11, ()),
'[' => (11, ()),
_ => return None,
};
Some(res)
}
fn infix_binding_power(op: char) -> Option<(u8, u8)> {
let res = match op {
'=' => (2, 1),
'?' => (4, 3),
'+' | '-' => (5, 6),
'*' | '/' => (7, 8),
'.' => (14, 13...
News & Blog Posts
2020-05-05
~18 min read
rust-lang.github.io
Summary
Rust's support for pattern matching on slices has grown steadily and incrementally without a lot of oversight. We have concern that Rust is doing too much here, and that the complexity is not worth it. This RFC proposes to feature gate multiple-element slice matches in the head...
RFC 164
RFC
2014-07-14
~1 min read
rust-lang.github.io
...Adding this hint to enums will force downstream crates to add a wildcard arm to match statements, ensuring that adding new variants is not a breaking change.
Adding this hint to structs or enum variants will prevent downstream crates from constructing or exhaustively matching, to ensure that adding new fields...
RFC 2008
RFC
2017-05-24
~12 min read
doc.rust-lang.org
Enums and Pattern Matching
In this chapter, we’ll look at enumerations, also referred to as enums. Enums allow you to define a type by enumerating its possible variants. First we’ll define and use an enum to show how an enum can encode meaning along with data. Next, we...
The Rust Programming Language
Book
2024-02-01
~1 min read
rust-lang-nursery.github.io
...It hands back the absolute path of the first match.
which resolves a single best match. which_all yields every match in precedence order. That helps when a name is shadowed by more than one install. A name that is not on the PATH comes back as an Error. A...
The Rust Cookbook
Book
2024-01-01
~1 min read
blog.knoldus.com
...i32) -> i32 {
match number {
2 => number,
_ => number + 1 ,
}
}
const DIGIT: i32 = 9;
const RESULT: i32 = even(DIGIT);
const RESULT_MATCH: i32 = even_no(DIGIT);
fn main() {
println!("The result of const function with if statement: {}", RESULT);
println!("The result of const function with match statement: {}", RESULT_MATCH);
}
Output:
The result...
Learn Simple Rust
2020-09-30
~2 min read
docs.rs
...grok The grok library allows you to quickly parse and match potentially unstructured data into a structed result. It is especially helpful when parsing logfiles of all kinds. This Rust version is mainly a port from the Java version which in turn drew inspiration from the original Ruby version . Usage...
Crate
v2.4.1
2026-03-19
rustc-dev-guide.rust-lang.org
...If you'd like to avoid whitespace normalization and / or if you'd like to match with a regex, use matchesraw instead.
matches
Usage: //@ matches PATH XPATH PATTERN
Checks that the text of each element / attribute / text selected by XPATH in the file given by PATH matches the Python-flavored...
Guide to Rustc Development
Book
2024-01-01
~5 min read
aldaronlau.com
...Match Statements
Match statements are my favorite feature of Rust (by far).
Doesn't mean they can't be improved though. Someone who is
unfamiliar with Rust might write:
fn main() {
let mut a = 4;
match 5 {
a => unreachable!(),
b => println!("{}", b),
}
}
This code panics (which I still get confused...
Call for Blog Posts
2020-09-30
~8 min read
doc.rust-lang.org
...u32) -> List {
// `Cons` also has type List
Cons(elem, Box::new(self))
}
// Return the length of the list
fn len(&self) -> u32 {
// `self` has to be matched, because the behavior of this method
// depends on the variant of `self`
// `self` has type `&List`, and `*self` has type `List`, matching on...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
...Food) -> Option<Food> {
match food {
Food::CordonBleu => None,
_ => Some(food),
}
}
// To make a dish, we need both the recipe and the ingredients.
// We can represent the logic with a chain of `match`es:
fn cookable_v1(food: Food) -> Option<Food> {
match have_recipe(food) {
None => None,
Some(food) => have_ingredients...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
pub struct SplitInclusiveMut<'a, T, P>
SplitInclusiveMut — An iterator over the mutable subslices of the vector which are separated by elements that match pred. Unlike SplitMut, it contains the matched parts in the ends of the subslices.
This struct is created by the split_inclusive_mut method on slices.
Example
let mut v = [10, 40, 30...
struct
core
Stable since 1.51.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn split_inclusive_mut<F>(&mut self, pred: F) -> SplitInclusiveMut<'_, T, F>
slice::split_inclusive_mut — Returns an iterator over mutable subslices separated by elements that match pred. The matched element is contained in the previous subslice as a terminator.
Examples
let mut v = [10, 40, 30, 20, 60, 50];
for group in v.split_inclusive_mut(|num| *num % 3 == 0) {
let...
method
core
Stable since 1.51.0
Version 1.100.0-nightly
doc.rust-lang.org
...let [[x]] = &[&mut [()]]; // x: &()
Patterns such as this are said to be using match ergonomics, originally introduced in RFC 2005.
Under match ergonomics, as we incrementally match a pattern against a scrutinee, we keep track of the default binding mode. This mode can be one of move, ref mut, or...
The Rust Edition Guide
Book
2024-01-01
~4 min read
doc.rust-lang.org
pub fn binary_search(&self, x: &T) -> Result<usize, usize>
...If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could...
method
alloc
Stable since 1.54.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct SplitInclusive<'a, T, P>
SplitInclusive — An iterator over subslices separated by elements that match a predicate function. Unlike Split, it contains the matched part as a terminator of the subslice.
This struct is created by the split_inclusive method on slices.
Example
let slice = [10, 40, 33, 20];
let mut iter = slice.split_inclusive...
struct
core
Stable since 1.51.0
Version 1.100.0-nightly
rust-analyzer.github.io
...in "Implement default members" and "Convert #[derive] to manual impl".
#15376 make "Convert match to matches!" assist trigger on non-literal bool arms.
#15345 don’t provide add_missing_match_arms assist when up-mapping match arm list failed.
#15397 remove unwraps from "Generate delegate trait".
#15406 don’t provide...
Project/Tooling Updates
2023-08-09
~1 min read