File tree 1 file changed +15
-3
lines changed
compiler/rustc_error_codes/src/error_codes
1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -16,18 +16,30 @@ The `Default` cannot be derived on an enum for the simple reason that the
16
16
compiler doesn't know which value to pick by default whereas it can for a
17
17
struct as long as all its fields implement the ` Default ` trait as well.
18
18
19
- If you still want to implement ` Default ` on your enum , you'll have to do it "by
20
- hand" :
19
+ For the case where the desired default variant has no data , you can annotate
20
+ it with ` #[default] ` to derive it :
21
21
22
22
```
23
+ #[derive(Default)]
23
24
enum Food {
25
+ #[default]
24
26
Sweet,
25
27
Salty,
26
28
}
29
+ ```
30
+
31
+ In the case where the default variant does have data, you will have to
32
+ implement ` Default ` on your enum "by hand":
33
+
34
+ ```
35
+ enum Food {
36
+ Sweet(i32),
37
+ Salty,
38
+ }
27
39
28
40
impl Default for Food {
29
41
fn default() -> Food {
30
- Food::Sweet
42
+ Food::Sweet(1)
31
43
}
32
44
}
33
45
```
You can’t perform that action at this time.
0 commit comments