ncameron.org
...As in a match expression, => indicates matching a pattern (in this case ()) to a body (in this case println!(...);). Since our use of the macro matches the single pattern (hello!() has no arguments), we expand into the given body.
The next example has some arguments, but still a single rule...
News & Blog Posts
2015-11-09
~4 min read
doc.rust-lang.org
pub fn split_inclusive<F>(&self, pred: F) -> SplitInclusive<'_, T, F>
slice::split_inclusive — Returns an iterator over subslices separated by elements that match pred. The matched element is contained in the end of the previous subslice as a terminator.
Examples
let slice = [10, 40, 33, 20];
let mut iter = slice.split_inclusive(|num| num % 3 == 0);
assert_eq!(iter.next...
method
core
Stable since 1.51.0
Version 1.100.0-nightly
estada.ch
...Argument dependant lookup in match
unwrap_all()
Coupling split() and join()
Backwards compatibility
Argument dependant lookup in match
Consider this code for talking to a secondary processor:
#[derive(Debug)]
pub enum SecondaryProcessor {
Opening(String),
Ready,
Writing(String),
Reading(String),
Closing,
}
When we match on it today, we have to do...
Call for Blog Posts
2020-09-23
~4 min read
doc.rust-lang.org
pub fn binary_search_by<'a, F>(&'a self, f: F) -> 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 enum ErrorKind
...This list is intended to grow over time and it is not recommended to exhaustively match against it.
It is used with the io::Error type.
Handling errors and matching on ErrorKind
In application code, use match for the ErrorKind values you are expecting; use _ to match "all other errors...
enum
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub const 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. The index is chosen deterministically, but is subject to change in future versions of Rust. If the value is...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn rsplit<F>(&self, pred: F) -> RSplit<'_, T, F>
slice::rsplit — Returns an iterator over subslices separated by elements that match pred, starting at the end of the slice and working backwards. The matched element is not contained in the subslices.
Examples
let slice = [11, 22, 33, 0, 44, 55];
let mut iter = slice.rsplit(|num| *num == 0);
assert...
method
core
Stable since 1.27.0
Version 1.100.0-nightly
github.com
...a `match` statement takes one expression and applies multiple patterns to it until one matches,
while let-else-else-chains would take one pattern and apply it to multiple expressions until one matches.
This has a complexity issue with or-patterns, where expressions can _easily_ become exponential.
(This is already...
RFC 3137
RFC
2021-05-31
~21 min read
doc.rust-lang.org
pub fn split_inclusive<P>(&self, pat: P) -> SplitInclusive<'_, P>
str::split_inclusive — Returns an iterator over substrings of this string slice, separated by characters matched by a pattern.
Differs from the iterator produced by split in that split_inclusive leaves the matched part as the terminator of the substring.
The pattern can be a &str, char, a slice of...
method
core
Stable since 1.51.0
Version 1.100.0-nightly
doc.rust-lang.org
...match PrintOnDrop("Matched value in final expression") {
// Non-pattern-matching guards' temporaries are dropped once the
// condition has been evaluated
_ if PrintOnDrop("guard condition").0 == "" => (),
// Pattern-matching guards' temporaries are dropped when leaving the
// arm's scope
_ if let "guard scrutinee" = PrintOnDrop("guard scrutinee").0 => {
let _ = &PrintOnDrop("lifetime-extended temporary...
The Rust Reference
Book
2024-01-01
~16 min read
doc.rust-lang.org
pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> 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 const fn binary_search_by<'a, F>(&'a self, f: F) -> 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. The index is chosen deterministically, but is subject to change in future versions of Rust. If the value is...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, T, F>
slice::split_mut — Returns an iterator over mutable subslices separated by elements that match pred. The matched element is not contained in the subslices.
Examples
let mut v = [10, 40, 30, 20, 60, 50];
for group in v.split_mut(|num| *num % 3 == 0) {
group[0] = 1;
}
assert_eq!(v...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct SplitInclusive<'a, P>
SplitInclusive — An iterator over the substrings of a string, terminated by a substring matching to 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 str. See its documentation for more.
struct
core
Stable since 1.51.0
Version 1.100.0-nightly
doc.rust-lang.org
pub const fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> 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. The index is chosen deterministically, but is subject to change in future versions of Rust. If the value is...
method
core
Stable since 1.10.0
Version 1.100.0-nightly
github.com
## 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
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
dannas.name
...fn match_here(regex: &[u8], text: &[u8]) -> bool {
match (regex, text) {
(&[], _) => true,
(&[b'$'], &[]) => true,
(&[b'$'], _) => false,
(&[r, b'*', ref rs @ ..], txt) => {
match_star(r, rs, txt)
}
(&[_, ref _s @ ..], &[]) => false,
(&[r, ref rs @ ..], &[t, ref ts @ ..]) if r == b'.' || r == t => {
match_here(rs, ts)
}
_ => false,
}
}
Finally we have the matching...
Rust Walkthroughs
2022-09-14
~7 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