macro

matches! — Turn a pattern test into a boolean

Test whether an expression fits a match pattern and optional guard, yielding bool. The bindings belong to the pattern test, not the surrounding scope. Matching a value can move its contents, so borrow it if the original must remain usable. Expression result: bool. Expected result (not executed): stdout: 'Express: true\n'; no stderr. Common mistake: Expecting the pattern variable days to exist after matches!. Use if let or match to work with the extracted value.

Formal Syntax & Structure

rust syntaxrust
matches!(expression, pattern) or matches!(expression, pattern if guard)

Working Example

Example Snippetrust
fn main() {    let delivery = Some(3);    let express = matches!(delivery, Some(days) if days <= 4);    println!("Express: {express}");}
Rust Reference