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

Recently updated

#202 202. Arc::clone Is a Refcount Bump, Not a Deep Copy — Share Big Data, Don't Duplicate It #203 203. Peekable::next_if_map — Consume a Token Only If It Parses, Transform in One Step #204 204. take_while / skip_while — Act on the Leading Run, Not Every Match #200 200. #[derive(Copy)] and #[inline] — Make Small Types Free to Pass Around #201 201. or_insert vs or_insert_with — Don't Build a Default You'll Throw Away

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