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 Iterators Iterator Collections Performance Option Slices Strings Vec Integers Result Parsing Concurrency Rust-1.95 Error-Handling Sync Arrays Math Overflow Ownership Slice Sorting Functional Interior-Mutability Mem Stdlib Hashmap Pattern-Matching Smart-Pointers Allocation Arc Cow Ffi Lazylock Path Rust-1.93 Async Attributes Borrow-Checker Btreemap Cell Closures Debugging Entry Ergonomics Filesystem Generics Idioms Io Iter Rust-1.94 Search Algorithms Api-Design Arithmetic Atomics Bit-Manipulation Char Default Enum Float Future Hints Impl-Trait Lazy Lazycell Macro Match Maybeuninit Nonzero Numerics Oncelock Patterns Peekable Pin Ranges Rc Rust-1.96 Rust-2024 Rwlock Smart-Pointer State-Machine Static Testing Traits Utf-8 Vecdeque Alignment Array-Windows Atomic Benchmarking Binary-Heap Binaryheap Bool Box Break Bytes Casting Cfg Clippy Closure Cmp Collect Combinators Comparison Compile-Time Compiler Conditional-Compilation Const Const-Generics Constants Control-Flow Controlflow Conversions Copy Cross-Platform Data-Processing Deref Diagnostics Dispatch Display Drain Duration Enumerate Enums Error-Messages File File-Locking Filter Flatten Floats Fmt Fn Fnmut Fnonce Fold Formatting From_fn Fs Gotcha Guards Hashset 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 Oncecell Optimization Ord Ordering Panic Panics Pathbuf Pettern Phantom-Data Pin-Project Pipes Position Precision Processes Raw String Refcell Rust-1.79 Rust-1.88 Rust-1.91 Scan Scoped-Threads Sort Statics Str String Structs Syntax Threads Time Tokio Trait-Objects Try-Fold Type-Inference Type-Safety Types Unicode Unsafe Unsafecell Unzip Upcasting Validation Weak Zero-Sized-Types

Recently updated

#237 237. Iterator::eq — Compare Two Sequences Without Collecting Them First #238 238. slice::chunks_exact_mut — Edit a Slice in Fixed-Size Blocks Without Index Math #239 239. Vec::drain — Remove a Range and Keep What You Pulled Out #240 240. to_le_bytes / from_le_bytes — Serialize Integers Without unsafe or Bit-Shifting #241 241. slice::split_first — Peel the Head Off a Slice, Keep the Tail, No Indexing

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