File tree Expand file tree Collapse file tree 1 file changed +15
-3
lines changed
compiler/rustc_error_codes/src/error_codes Expand file tree Collapse file tree 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
1616compiler doesn't know which value to pick by default whereas it can for a
1717struct as long as all its fields implement the ` Default ` trait as well.
1818
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 :
2121
2222```
23+ #[derive(Default)]
2324enum Food {
25+ #[default]
2426 Sweet,
2527 Salty,
2628}
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+ }
2739
2840impl Default for Food {
2941 fn default() -> Food {
30- Food::Sweet
42+ Food::Sweet(1)
3143 }
3244}
3345```
You can’t perform that action at this time.
0 commit comments