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 Std Iterator Collections Iterators Option Vec Slices Concurrency Rust-1.95 Performance Result Sync Arrays Error-Handling Sorting Functional Integers Interior-Mutability Math Stdlib Strings Mem Ownership Ffi Hashmap Overflow Parsing Pattern-Matching Rust-1.93 Slice Smart-Pointers Arc Async Attributes Cell Closures Io Iter Lazylock Rust-1.94 Algorithms Arithmetic Atomics Borrow-Checker Btreemap Cow Debugging Entry Enum Filesystem Future Hints Lazy Lazycell Macro Match Maybeuninit Oncelock Path Patterns Pin Rc Rust-2024 Rwlock Smart-Pointer Static Testing Traits Vecdeque Array-Windows Atomic Benchmarking Binary-Heap Binaryheap Bool Box Break Casting Cfg Char Clippy Closure Cmp Collect Combinators Compile-Time Compiler Conditional-Compilation Const Const-Generics Constants Controlflow Conversions Cross-Platform Data-Processing Default Diagnostics Display Drain Duration Enumerate Enums Ergonomics Error-Messages File File-Locking Filter Flatten Float Floats Fn Fnmut Fnonce Fold Formatting From_fn Fs Generics Guards If-Let Impl-Trait Initialization Intersperse Lazy-Init Let-Chains Lifetime Lifetimes Lints Loop Macros Map Map_while Match-Guards Matching Memory Mutex Niche Nonzero Num Numerics Oncecell Optimization Ordering Panic Panics Peekable Pettern Phantom-Data Pin-Project Pipes Position Precision Processes Ranges Raw String Refcell Rust-1.79 Rust-1.88 Rust-1.91 Rust-1.96 Scan Scoped-Threads Search Sort State-Machine Statics String Threads Time Tokio Trait-Objects Try-Fold Type-Inference Type-Safety Types Unicode Unsafe Unsafecell Unzip Upcasting Utf-8 Validation Weak Zero-Sized-Types

Recently updated

#173 173. NonZeroU32 and Friends — Encode an Invariant and Shrink Option for Free #171 171. assert_matches! — A Test Failure That Actually Tells You What Went Wrong #172 172. #[track_caller] — Point the Panic at the Caller, Not Your Helper #170 170. Ordering::then_with — Chain Comparators for Multi-Key Sorts #167 167. Saturating<T> — Stop Calling .saturating_add() Everywhere

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