2 The `Default` trait supports the following attributes:
4 * **Container attributes**
5 * [`Default(bound="<where-clause or empty>")`](#custom-bound)
6 * [`Default="new"`](#new-function)
7 * **Variant attributes**
8 * [`Default`](#default-enumeration)
10 * [`Default(bound="<where-clause or empty>")`](#custom-bound)
11 * [`Default(value="<expr>")`](#setting-the-value-of-a-field)
15 You can use *derivative* to derive a default implementation on enumerations!
16 This does not work with *rustc*'s `#[derive(Default)]`.
17 All you need is to specify what variant is the default value:
20 # extern crate derivative;
21 # use derivative::Derivative;
22 #[derive(Debug, Derivative)]
23 #[derivative(Default)]
26 #[derivative(Default)]
30 println!("{:?}", Enum::default()); // B
33 # Setting the value of a field
35 You can use *derivative* to change the default value of a field in a `Default`
39 # extern crate derivative;
40 # use derivative::Derivative;
41 #[derive(Debug, Derivative)]
42 #[derivative(Default)]
45 #[derivative(Default(value="42"))]
49 println!("{:?}", Foo::default()); // Foo { foo: 0, bar: 42 }
54 You can use *derivative* to derive a convenience `new` method for your type
55 that calls `Default::default`:
58 # extern crate derivative;
59 # use derivative::Derivative;
60 #[derive(Debug, Derivative)]
61 #[derivative(Default(new="true"))]
67 println!("{:?}", Foo::new()); // Foo { foo: 0, bar: 0 }
72 The following does not work because `derive` adds a `T: Default` bound on the
73 `impl Default for Foo<T>`:
76 # extern crate derivative;
77 # use derivative::Derivative;
85 Foo::<NonDefault>::default(); // gives:
86 // error: no associated item named `default` found for type `Foo<NonDefault>` in the current scope
87 // = note: the method `default` exists but the following trait bounds were not satisfied: `NonDefault : std::default::Default`
90 That bound however is useless as `Option<T>: Default` for any `T`.
91 `derivative` allows you to explicitly specify a bound if the inferred one is not
95 # extern crate derivative;
96 # use derivative::Derivative;
98 #[derivative(Default(bound=""))] // don't need any bound
105 Foo::<NonDefault>::default(); // works!