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

Recently updated

#302 302. cbrt — powf(1.0/3.0) Says the Cube Root of -27 Is NaN #303 303. round_ties_even — round() Pushes Every Half Up, and Your Totals Drift #304 304. trunc & fract — Split a Float Without the Cast Round-Trip #305 305. to_bits — Hash and Dedup Floats Without a Wrapper Crate #306 306. powi — Integer Powers Without the powf Detour

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