Skip to content

Commit 6730537

Browse files
authored
Rollup merge of #94457 - jhpratt:stabilize-derive_default_enum, r=davidtwco
Stabilize `derive_default_enum` This stabilizes `#![feature(derive_default_enum)]`, as proposed in [RFC 3107](rust-lang/rfcs#3107) and tracked in #87517. In short, it permits you to `#[derive(Default)]` on `enum`s, indicating what the default should be by placing a `#[default]` attribute on the desired variant (which must be a unit variant in the interest of forward compatibility). ```````@rustbot``````` label +S-waiting-on-review +T-lang
2 parents b39e6f1 + 4f0b6df commit 6730537

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

core/src/default.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@
5252
/// This trait can be used with `#[derive]` if all of the type's fields implement
5353
/// `Default`. When `derive`d, it will use the default value for each field's type.
5454
///
55+
/// ### `enum`s
56+
///
57+
/// When using `#[derive(Default)]` on an `enum`, you need to choose which unit variant will be
58+
/// default. You do this by placing the `#[default]` attribute on the variant.
59+
///
60+
/// ```
61+
/// #[derive(Default)]
62+
/// enum Kind {
63+
/// #[default]
64+
/// A,
65+
/// B,
66+
/// C,
67+
/// }
68+
/// ```
69+
///
70+
/// You cannot use the `#[default]` attribute on non-unit or non-exhaustive variants.
71+
///
5572
/// ## How can I implement `Default`?
5673
///
5774
/// Provide an implementation for the `default()` method that returns the value of

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
#![feature(const_precise_live_drops)]
168168
#![feature(const_refs_to_cell)]
169169
#![feature(decl_macro)]
170-
#![feature(derive_default_enum)]
170+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
171171
#![feature(deprecated_suggestion)]
172172
#![feature(doc_cfg)]
173173
#![feature(doc_notable_trait)]

0 commit comments

Comments
 (0)