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

#261 261. String::retain — Delete Characters In Place, No New Allocation #262 262. String::insert_str — Prepend or Splice Text Without Rebuilding the String #260 260. str::replacen — Replace the First N Matches, Not Every Single One #257 257. VecDeque::rotate_left — Where the Ring Buffer Finally Pays Rent #258 258. split_whitespace — Split on Runs, Not on Every Single Space

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