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 Option Iterators Collections Rust-1.95 Vec Performance Slices Functional Sorting Stdlib Arrays Error-Handling Result Integers Math Parsing Slice Strings Concurrency Iter Ownership Pattern-Matching Algorithms Attributes Borrow-Checker Closures Debugging Enum Hashmap Hints Io Lazy Lazylock Macro Match Mem Overflow Patterns Rust-1.94 Rust-2024 Smart-Pointers Static Traits Vecdeque Arc Arithmetic Array-Windows Async Atomics Benchmarking Binaryheap Bool Box Break Casting Cell Cfg Clippy Closure Combinators Compile-Time Compiler Conditional-Compilation Const Const-Generics Constants Controlflow Conversions Cow Cross-Platform Data-Processing Default Diagnostics Display Drain Entry Enumerate Error-Messages File-Locking Filter Flatten Float Floats Fn Fnmut Fnonce Fold Formatting From_fn Guards If-Let Impl-Trait Initialization Interior-Mutability Intersperse Lazycell Let-Chains Lifetime Lifetimes Lints Loop Map Map_while Match-Guards Matching Maybeuninit Memory Numerics Optimization Panics Peekable Pettern Pipes Position Precision Processes Ranges Raw String Rust-1.88 Scan Scoped-Threads Search State-Machine String Threads Tokio Trait-Objects Try-Fold Type-Inference Type-Safety Unsafe Unzip Upcasting Utf-8 Validation

Recently updated

#112 112. Iterator::scan — Fold That Yields Every Step #110 110. slice::split_at_checked — Split Without the Panic #111 111. Vec::insert_mut — Splice In and Edit Without Reindexing #109 109. BinaryHeap::peek_mut — Edit the Top Without Pop-and-Push #107 107. Vec::splice — Replace a Range and Keep the Old Values

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