rustbites rustbites bite-sized rust tips
Archives About
#016 Jun 10, 2022
option result matching

16. Option/Result match?!

Try to avoid matching Option or Result.

Use if let instead.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
let result = Some(111);

// Not nice
match result {
    Some(x) => println!("{x}"),
    None => {}
};

// Better
if let Some(x) = result {
    println!("{x}");
}

This post is licensed under CC BY 4.0 by the author.

Share:
← Previous 14. Find index of item Next → 18. flatten options

Trending Tags

Rust Iterator Option Iter Iterators Result Collections Enum Macro Match Std Vec Array-Windows Arrays Async Attributes Break Closure Closures Combinators Compiler Concurrency Cow Debugging Default Drain Entry Enumerate Error-Handling Filter Flatten Fn Fnmut Fnonce Fold From_fn Functional Hashmap If-Let Initialization Lazy Lazylock Loop Map Matching Mem Ownership Pattern-Matching Performance Pettern Position Raw String Scan Scoped-Threads Slices Smart-Pointers Static Stdlib String Strings Threads Tokio Trait-Objects Traits Upcasting

Recently updated

#043 43. Vec::extract_if — Remove Elements and Keep Them #042 42. array::from_fn — Build Arrays from a Function #041 41. Async Closures — Pass Async Code Like Any Other Closure #040 40. Scoped Threads — Borrow Across Threads Without Arc #039 39. Trait Upcasting — Cast dyn Trait to dyn Supertrait

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