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

Recently updated

#232 232. Iterator::partition — Split Into Two Collections in One Pass #233 233. str::split_terminator — Split Without the Trailing Empty String #234 234. core::range::Range — A Range That's Copy, So You Can Store and Reuse It #235 235. Iterator::rposition — Find the Last Match Without Reversing-and-Subtracting #236 236. Result::flatten — Collapse a Result<Result<T, E>, E> in One Call

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