doc.rust-lang.org
pub struct Matches<'a, P>
Matches — Created with the method matches.
struct
core
Stable since 1.2.0
Version 1.100.0-nightly
doc.rust-lang.org
pub macro matches!
matches — Returns whether the given expression matches the provided pattern.
The pattern syntax is exactly the same as found in a match arm. The optional if guard can be used to add additional checks that must be true for the matched value, otherwise this macro will return false.
When testing...
macro
core
Stable since 1.42.0
Version 1.100.0-nightly
docs.rs
A macro to evaluate, as a boolean, whether an expression matches a pattern. A macro to evaluate, as a boolean, whether an expression matches a pattern. For users who build using only Rust 1.42 and newer, consider using std::matches , which is included in the standard library prelude and...
Crate
v0.1.10
2023-01-21
doc.rust-lang.org
match expressions
MatchExpression ->
`match` Scrutinee `{`
InnerAttribute*
MatchArms?
`}`
Scrutinee -> Expression _except [StructExpression]_
MatchArms ->
( MatchArm `=>` ( ExpressionWithoutBlock `,` | ExpressionWithBlock `,`? ) )*
MatchArm `=>` Expression `,`?
MatchArm -> OuterAttribute* Pattern MatchArmGuard?
MatchArmGuard -> `if` MatchConditions
MatchConditions ->
MatchGuardChain
| Expression
MatchGuardChain -> MatchGuardCondition ( `&&` MatchGuardCondition )*
MatchGuardCondition ->
Expression _except [ExcludedMatchConditions]_
| OuterAttribute* `let` Pattern `=` MatchGuardScrutinee
MatchGuardScrutinee -> Expression _except [ExcludedMatchConditions]_
@root ExcludedMatchConditions ->
LazyBooleanExpression
| RangeExpr
| RangeFromExpr
| RangeInclusiveExpr
| AssignmentExpression
| CompoundAssignmentExpression...
The Rust Reference
Book
2024-01-01
~6 min read
doc.rust-lang.org
pub macro debug_assert_matches!
debug_assert_matches — Asserts that an expression matches the provided pattern.
This macro is generally preferable to debug_assert!(matches!(value, pattern)), because it can print the debug representation of the actual value shape that did not meet expectations. In contrast, using debug_assert! will only print that expectations were...
macro
core
Stable since 1.96.0
Version 1.100.0-nightly
doc.rust-lang.org
pub macro assert_matches!
assert_matches — Asserts that an expression matches the provided pattern.
This macro is generally preferable to assert!(matches!(value, pattern)), because it can print the debug representation of the actual value shape that did not meet expectations. In contrast, using assert! will only print that expectations were not met, but...
macro
core
Stable since 1.96.0
Version 1.100.0-nightly
docs.rs
Cross platform single glob and glob set matching. Glob set matching is the
process of matching one or more glob patterns against a single candidate path
simultaneously, and returning all of the globs that matched.
globset Cross platform single glob and glob set matching. Glob set matching is the process...
Crate
v0.4.20
2026-08-04
docs.rs
...The matches are tried in order. The first matches get priority over later ones, even if later ones are perfect matches. let x = 123i32; let result = spez! { for x; match<T> T where i8: From<T> -> i32 { 0 } match<T: std::fmt::Debug> T -> i32 { 1 } match i32 -> i32 { 2...
Crate
v0.1.2
2023-04-26
doc.rust-lang.org
Argument parsing
Matching can be used to parse simple arguments:
use std::env;
fn increase(number: i32) {
println!("{}", number + 1);
}
fn decrease(number: i32) {
println!("{}", number - 1);
}
fn help() {
println!("usage:
match_args <string>
Check whether given string is the answer.
match_args {{increase|decrease}} <integer>
Increase or decrease given...
Rust by Example
Book
2024-01-01
~1 min read
crates.io
An extremely fast glob matcher glob-match An extremely fast glob matching library with support for wildcards, character classes, and brace expansion. Linear time matching. No exponential backtracking. Zero allocations. No regex compilation. Matching occurs on the glob pattern in place. Support for capturing matched ranges of wildcards. Thousands of...
Crate
v0.2.1
2023-02-07
rustc-dev-guide.rust-lang.org
Pattern and exhaustiveness checking
In Rust, pattern matching and bindings have a few very helpful properties. The compiler will check that bindings are irrefutable when made and that match arms are exhaustive.
Pattern usefulness
The central question that usefulness checking answers is: "in this match expression, is that branch redundant...
Guide to Rustc Development
Book
2024-01-01
~6 min read
blog.knoldus.com
...matches macro!
matches(macro) is provided by Rust’s standard library and an external crate called matches.Here we’ll talk about the matches macro provided by our standard library only, although both matches provide the same functionality but let’s focus on the official one.
So matches checks whether...
Rust Walkthroughs
2020-12-30
~2 min read
docs.rs
Asserts that a value matches a pattern assert_matches Provides a macro, assert_matches , which tests whether a value matches a given pattern, causing a panic if the match fails. Documentation #[macro_use] extern crate assert_matches; #[derive(Debug)] enum Foo { A(i32), B(i32), } let a = Foo::A(1...
Crate
v1.5.0
2021-02-05
blog.cuongle.dev
Rust’s pattern matching feels simple enough: match on enums, destructure tuples, handle Option and Result. I stuck with these basics for months because, well, they work. But there’s a whole world of pattern techniques I wasn’t using. Once I discovered what’s actually possible, I kicked myself...
Rust Walkthroughs
2025-10-01
~4 min read
doc.rust-lang.org
...We’ll talk about match guards later in the “Adding Conditionals with Match Guards” section.
Matching Multiple Patterns
In match expressions, you can match multiple patterns using the | syntax, which is the pattern or operator. For example, in the following code, we match the value of x against the match...
The Rust Programming Language
Book
2024-02-01
~19 min read
doc.rust-lang.org
match
Rust provides pattern matching via the match keyword, which can be used like a C switch. The first matching arm is evaluated and all possible values must be covered.
fn main() {
let number = 13;
// TODO ^ Try different values for `number`
println!("Tell me about {}", number);
match number {
// Match a...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
pub fn match_indices<P>(&self, pat: P) -> MatchIndices<'_, P>
str::match_indices — Returns an iterator over the disjoint matches of a pattern within this string slice as well as the index that the match starts at.
For matches of pat within self that overlap, only the indices corresponding to the first match are returned.
The pattern can be a...
method
core
Stable since 1.5.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn trim_matches<P>(&self, pat: P) -> &str
str::trim_matches — Returns a string slice with all prefixes and suffixes that match a pattern repeatedly removed.
The pattern can be a char, a slice of chars, or a function or closure that determines if a character matches.
Examples
Simple patterns:
assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
assert...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
docs.rs
Wildcard matching wildcard wildcard is a rust crate for wildcard matching. Here's how to use it: let wildcard = Wildcard::new("*foo?*bar".as_bytes()).unwrap(); assert!(wildcard.is_match("fooofooobar".as_bytes())); Special characters can be escaped to represent their literal symbol: let wildcard = Wildcard::new(r"\*\?".as_bytes...
Crate
v0.3.0
2024-11-14
github.com
...Each match includes the pattern that matched along with the byte offsets of the match. use aho_corasick::{AhoCorasick, PatternID}; let patterns = &["apple", "maple", "Snapple"]; let haystack = "Nobody likes maple in their apple flavored Snapple."; let ac = AhoCorasick::new(patterns).unwrap(); let mut matches = vec![]; for mat in ac.find...
Crate
v1.1.5
2026-08-03