doc.rust-lang.org
pub fn ends_with<P>(&self, pat: P) -> bool
str::ends_with — Returns true if the given pattern matches a suffix of this string slice.
Returns false if it does not.
The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.
Examples
let bananas = "bananas";
assert!(bananas...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
flinect.com
...One macro I really like is matches!.
It's basically a single-arm match expression, but you can readably use it in if statements.
So, this:
match data_type.id {
ParsedDataTypeId::Standard(ref data_type_id) if data_type_id == "data-trait-name" => {
// ...
}
_ => {}
}
Can be written as:
if matches!(data...
Rust Walkthroughs
2023-11-29
~6 min read
rust-lang.github.io
...Some libraries (like Python's re and RE2/C++) distinguish between matching an expression against an entire string and matching an expression against part of the string. My implementation favors simplicity: matching the entirety of a string requires using the ^ and/or $ anchors. In all cases, an implicit .*? is added...
RFC 42
RFC
2014-04-12
~9 min read
rust-lang.github.io
...i32 = match some_bool {
true => 23,
false => panic!("aaah!"), // an expression of type `!`, gets cast to `i32`
}
match break {
() => 23, // matching with a `()` forces the match argument to be cast to type `()`
}
These casts can be implemented by having the compiler assign a fresh, diverging type variable to any expression...
RFC 1216
RFC
2015-07-19
~12 min read
blog.logrocket.com
...Understanding match in Rust
Feel free to skip to the next section if you already understand pattern matching.
Before exploring how that’s even possible, let’s understand Rust’s idea of pattern matching. Here’s a scenario:
A hungry customer asks for a meal from our Korean street food...
Rust Walkthroughs
2021-07-28
~11 min read
kivooeo.github.io
...app <command>".to_string(),
| ^^^^^^^^ pattern cannot match with input type `Vec<String>`
|
help: consider slicing here
|
2 | match &args[..] {
| ++++
Now the slice pattern matches the slice, but the "help" pattern still doesn't match a String element, because Rust doesn't automatically dereference the String to match it as a...
Observations/Thoughts
2026-10-28
~9 min read
gsquire.github.io
...This is a powerful construct in that it can match multiple patterns that you provide in a single
scan. Using this idea, I thought it would be fun to write a router with a RegexSet matching
requests under the hood.
The result of this is reroute. This crate provides a...
News & Blog Posts
2016-06-20
~2 min read
github.com
...i32 = match some_bool {
true => 23,
false => panic!("aaah!"), // an expression of type `!`, gets cast to `i32`
}
match break {
() => 23, // matching with a `()` forces the match argument to be cast to type `()`
}
```
These casts can be implemented by having the compiler assign a fresh, diverging
type variable to any expression...
RFC 1216
RFC
2015-07-19
~13 min read
apanatshka.github.io
...Debug;
fn start_state() -> Self::State;
fn next_state(&self, state: &Self::State, input: &Input) -> Self::State;
fn get_match(&self, state: &Self::State, text_offset: usize) -> Option<Match<Payload>>;
fn find<'i, 'a>(&'a self, s: &'i [Input]) -> Matches<'i, 'a, Input, Payload, Self>
where Self: Sized
{
Matches {
aut...
News & Blog Posts
2016-10-04
~11 min read
blog.ezyang.com
...When match is involved, you can usually arrange for the misbehaving borrow to be performed outside of the match statement, in a new, non-overlapping lexical scope. This is easy when the relevant branch does not rely on any variables from the pattern-match by using short-circuiting control operators...
Announcements, etc
2013-12-22
~8 min read
doc.rust-lang.org
pub const fn eq_ignore_ascii_case(&self, other: &str) -> bool
str::eq_ignore_ascii_case — Checks that two strings are an ASCII case-insensitive match.
Same as to_ascii_lowercase(a) == to_ascii_lowercase(b), but without allocating and copying temporaries.
For Unicode-aware case-insensitive matching, consider str::eq_ignore_case_unnormalized.
Examples
assert!("Ferris".eq_ignore_ascii_case...
method
core
Stable since 1.23.0
Version 1.100.0-nightly
doc.rust-lang.org
...The second arm of the outer match stays the same, so the program panics on any error besides the missing file error.
Alternatives to Using match with Result<T, E>
That’s a lot of match! The match expression is very useful but also very much a primitive. In Chapter...
The Rust Programming Language
Book
2024-02-01
~18 min read
docs.rs
...u32 = match <u32 as TryFrom<u8>>::try_from(42) { | Ok(it) => it, | Err(unreachable) => unreachable, // Error, expected `u32`, found `Infallible` }; but the following doesn't! use ::never_say_never::Never; let x: u32 = match Ok::<_, Never>(42) { | Ok(it) => it, | Err(unreachable) => unreachable, };
Crate
v6.6.666
2022-01-19
docs.rs
...use croner::Cron; use chrono::Local; fn main() { // Parse cron expression let cron_all = Cron::from_str("18 * * * 5") .expect("Couldn't parse cron string"); // Compare cron pattern with current local time let time = Local::now(); let matches_all = cron_all.is_time_matching(&time).unwrap(); // Get next match let...
Crate
v5.0.1
2026-11-25
doc.rust-lang.org
...io::Result<()> {
match OpenOptions::new().create(true).write(true).open(path) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}
fn main() {
println!("`mkdir a`");
// Create a directory, returns `io::Result<()>`
match fs::create_dir("a") {
Err(why) => println!("! {:?}", why.kind()),
Ok(_) => {},
}
println!("`echo hello > a/b.txt`");
// The previous match can be...
Rust by Example
Book
2024-01-01
~2 min read
gruebelinchen.wordpress.com
...It uses a placeholder, which we call ActualT, to specify the type of the value being matched. Rust provides two ways to express this. One can define a generic trait with a type parameter:
123trait Matcher<ActualT> { fn matches(&self, actual: &ActualT) -> bool;}
Or one can add an associated type...
Observations/Thoughts
2023-06-07
~7 min read
rust-lang.github.io
...These fragment specifiers define what Rust syntax will be matched and bound to each metavariable. For example, the item fragment specifier matches an item, block matches a block expression, expr matches an expression, and so on.
As we add new features to Rust, sometimes we change its syntax. This means...
RFC 3531
RFC
2023-11-16
~7 min read
andygrove.io
...However, we still have to pattern match for dynamic behavior at runtime. We can’t pattern match on the BufferArrayData itself but instead have to pattern match on the separate type metadata.
fn add(a: &ArrayData, a_type: DataType, b: &ArrayData, b_type: DataType) -> Rc<ArrayData> {
match (a_type, b...
News & Blog Posts
2018-05-08
~5 min read
saghm.github.io
...2, ..Default::default() };
Pattern match guards
Sometimes when pattern matching, the cases you want to handle
don’t map exactly to the patterns of the data you’re matching
on. For instance, you might write some code like this:
fn divide_opt(x: Option<i32>, y: Option<i32>) -> Option<i32...
News & Blog Posts
2019-04-02
~3 min read
erickt.github.io
...Consider what happens
with matches. Consider:
1
2
3
4
match ... {
x => { ... }
y => { ... }
}
Is x or y a variable, or a variant? There’s no way to know unless you
perform name resolution, otherwise known as the resolve pass in the compiler.
Unfortunately though, there’s no way for Stateful...
News & Blog Posts
2016-02-08
~10 min read