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

Recently updated

#302 302. cbrt — powf(1.0/3.0) Says the Cube Root of -27 Is NaN #303 303. round_ties_even — round() Pushes Every Half Up, and Your Totals Drift #304 304. trunc & fract — Split a Float Without the Cast Round-Trip #305 305. to_bits — Hash and Dedup Floats Without a Wrapper Crate #306 306. powi — Integer Powers Without the powf Detour

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