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 Iterators Collections Option Performance Vec Slices Strings Concurrency Rust-1.95 Result Sync Arrays Error-Handling Integers Ownership Parsing Sorting Functional Interior-Mutability Math Mem Stdlib Hashmap Pattern-Matching Smart-Pointers Arc Ffi Lazylock Overflow Path Rust-1.93 Slice Allocation Async Attributes Btreemap Cell Closures Cow Debugging Entry Ergonomics Filesystem Generics Io Iter Rust-1.94 Algorithms Api-Design Arithmetic Atomics Borrow-Checker Default Enum Float Future Hints Impl-Trait Lazy Lazycell Macro Match Maybeuninit Nonzero Numerics Oncelock Patterns Peekable Pin Rc Rust-2024 Rwlock Smart-Pointer State-Machine Static Testing Traits Utf-8 Vecdeque Array-Windows Atomic Benchmarking Binary-Heap Binaryheap Bit-Manipulation Bool Box Break Casting Cfg Char 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 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 Ranges Raw String Refcell Rust-1.79 Rust-1.88 Rust-1.91 Rust-1.96 Scan Scoped-Threads Search 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

#210 210. next_power_of_two — Round Up to a Power of Two Without the Bit-Twiddling #211 211. Iterator::inspect — Debug a Lazy Chain Without Tearing It Apart #205 205. strip_prefix / strip_suffix — Remove a Prefix Once, Not Every Repeat #206 206. trim_ascii — Trim Whitespace From &[u8] Without Going Through str #207 207. if let Guards — Match a Pattern Inside a Guard, and Still Fall Through

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