rustbites rustbites bite-sized rust tips
Archives About
#001 Jan 5, 2022
macro match

1. matches!

Need to check if an expression matches certain pattern?

1
2
let foo = vec!['1','2'];
assert!(matches!(foo.len(), 0..=1));

The example above checks if len is 0 or 1.

Or

1
2
3
let bar = Some(4);

assert!(matches!(bar, Some(x) if x > 2));

This post is licensed under CC BY 4.0 by the author.

Share:
← Previous 0. Hello, rustbites! Next → 2. mysterious @

Trending Tags

Rust Iterator Option Std Iterators Collections Functional Slices Arrays Iter Parsing Result Strings Vec Attributes Closures Concurrency Enum Hashmap Io Macro Match Math Pattern-Matching Rust-2024 Static Stdlib Array-Windows Async Borrow-Checker Box Break Cell Clippy Closure Combinators Compiler Const-Generics Constants Cow Data-Processing Debugging Default Display Drain Entry Enumerate Error-Handling File-Locking Filter Flatten Float Fn Fnmut Fnonce Fold Formatting From_fn Guards If-Let Impl-Trait Initialization Interior-Mutability Lazy Lazylock Let-Chains Lifetime Lifetimes Lints Loop Map Map_while Matching Mem Memory Ownership Peekable Performance Pettern Pipes Position Precision Processes Raw String Rust-1.88 Rust-1.94 Scan Scoped-Threads Slice Smart-Pointers String Threads Tokio Trait-Objects Traits Type-Inference Unzip Upcasting Utf-8 Validation

Recently updated

#067 67. Box::leak — Turn Owned Data Into a Static Reference #068 68. f64::next_up — Walk the Floating Point Number Line #066 66. Inferred Const Generics — Let the Compiler Count For You #064 64. Iterator::unzip — Split Pairs into Separate Collections #065 65. Precise Capturing — Stop impl Trait From Borrowing Too Much

About

A collection of bite-sized Rust tips and tricks. Small, focused lessons to help you write better Rust.

info@rustbites.com
rustbites rustbites · 2022—2026
X RSS

This site uses cookies for analytics and ads. Learn more