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

Recently updated

#183 183. std::mem::take — Move Out of &mut self Without the Clone #180 180. Option::unzip — Split an Optional Pair Into a Pair of Options #181 181. Option::get_or_insert_with — Lazy Default That Returns &mut #182 182. Path::with_extension — Swap a File Extension Without Slicing Strings #179 179. Iterator::max_by_key — Find the Best Element Without a Manual Fold

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