Skip to content

Commit 09693e6

Browse files
committed
Mention #[default] in E0655 code index
1 parent 4d2f468 commit 09693e6

File tree

1 file changed

+15
-3
lines changed
  • compiler/rustc_error_codes/src/error_codes

1 file changed

+15
-3
lines changed

compiler/rustc_error_codes/src/error_codes/E0665.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,30 @@ The `Default` cannot be derived on an enum for the simple reason that the
1616
compiler doesn't know which value to pick by default whereas it can for a
1717
struct 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)]
2324
enum 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
2840
impl Default for Food {
2941
fn default() -> Food {
30-
Food::Sweet
42+
Food::Sweet(1)
3143
}
3244
}
3345
```

0 commit comments

Comments
 (0)