Skip to content

Commit

Permalink
Mention #[default] in E0655 code index
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Dec 15, 2024
1 parent 8e10b56 commit b39e017
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions compiler/rustc_error_codes/src/error_codes/E0665.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@ The `Default` cannot be derived on an enum for the simple reason that the
compiler doesn't know which value to pick by default whereas it can for a
struct as long as all its fields implement the `Default` trait as well.

If you still want to implement `Default` on your enum, you'll have to do it "by
hand":
For the case where the desired default variant has no data, you can annotate
it with `#[default]` to derive it:

```
#[derive(Default)]
enum Food {
#[default]
Sweet,
Salty,
}
```

In the case where the default variant does have data, you will have to
implement `Default` on your enum "by hand":

```
enum Food {
Sweet(i32),
Salty,
}
impl Default for Food {
fn default() -> Food {
Food::Sweet
Food::Sweet(1)
}
}
```

0 comments on commit b39e017

Please sign in to comment.