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 Iterator Option Std Iterators Collections Functional Slices Iter Parsing Result Strings Vec Arrays Attributes Concurrency Enum Hashmap Io Macro Match Pattern-Matching Stdlib Array-Windows Async Borrow-Checker Break Cell Clippy Closure Closures Combinators Compiler Constants Cow Data-Processing Debugging Default Drain Entry Enumerate Error-Handling File-Locking Filter Flatten Fn Fnmut Fnonce Fold From_fn Guards If-Let Initialization Interior-Mutability Lazy Lazylock Let-Chains Lints Loop Map Map_while Matching Math Mem Ownership Peekable Performance Pettern Pipes Position Processes Raw String Rust-1.88 Rust-1.94 Rust-2024 Scan Scoped-Threads Slice Smart-Pointers Static String Threads Tokio Trait-Objects Traits Upcasting Utf-8 Validation

Recently updated

#060 60. Iterator::partition — Split a Collection in Two #061 61. Iterator::reduce — Fold Without an Initial Value #062 62. Iterator::flat_map — Map and Flatten in One Step #059 59. split_first_chunk — Destructure Slices into Arrays #057 57. New Math Constants — GOLDEN_RATIO and EULER_GAMMA in std

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