rustbites rustbites bite-sized rust tips
Archives About
#023 Dec 6, 2022
option iterator

23. Enum's default value

Instead of manually implementing Default trait for an enum, you can derive it and explicitly tell which variant should be the default one.

1
2
3
4
5
6
7
8
9
#[derive(Default)]
enum Car{
    #[default]
    Porsche,
    Ferrari,
    Skoda
}

let car = Car::default();

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

Share:
← Previous 22. Enum's Debug Next → 25. Option's iterator

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