Skip to content

Make sure empty struct always derives Default #72075

Closed
@sergey-melnychuk

Description

@sergey-melnychuk

I was wondering if requiring an explicit derive(Default) for empty struct makes sense. It feels right that empty (for whatever reason) struct should derive Default, well, by default :)

This is my first issue in rust-lang, so any feedback is welcome. If such suggestion makes sense, I'd like to implement it with very little guidance from more experienced Rustaceans.

//#[derive(Default)]
struct X;

fn f<T: Default>(_: T) {
    ()
}

fn main() {
    f(X);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `X: std::default::Default` is not satisfied
 --> src/main.rs:9:7
  |
4 | fn f<T: Default>(_: T) {
  |    -    ------- required by this bound in `f`
...
9 |     f(X);
  |       ^ the trait `std::default::Default` is not implemented for `X`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions