Why do all type parameters need to implement Default for #[derive(Default)] to work?

Consider this code:

struct Foo<T>(Option<T>);

struct DoesNotImplementDefault(u32);

use std::default::Default;
fn main() {
    let val = Foo::<DoesNotImplementDefault>::default();

This code does not compile. All fields on Foo<T> implement Default regardless of whether or not T does, but rustc insists that Foo does not implement Default unless T does. Why on Earth is this the case, and is it a bug in the compiler (or the Default derive macro)?

This is a known edge case - "perfect derive" is the keyword to search for. In short, it's not obvious how to make derived traits more applicable without having accidental semver hazards.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.