You can use the where clause with complicated constraints like where Option<T>: Default
instead of just T: Default
, but is there a reason why it shouldn't be possible to do something like the following example:
#[derive(Default)]
struct Test<A: Default, B: Default, C: Default> {
first: A,
second: B,
third: C,
}
impl<A, B, C> Test<A, B, C> where
(A, B, C): Default
{
// something
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `A: std::default::Default` is not satisfied
--> src/lib.rs:8:15
|
2 | struct Test<A: Default, B: Default, C: Default> {
| ------- required by this bound in `Test`
...
8 | impl<A, B, C> Test<A, B, C> where
| ^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `A`
|
help: consider further restricting type parameter `A`
|
9 | (A, B, C): Default, A: std::default::Default
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `B: std::default::Default` is not satisfied
--> src/lib.rs:8:15
|
2 | struct Test<A: Default, B: Default, C: Default> {
| ------- required by this bound in `Test`
...
8 | impl<A, B, C> Test<A, B, C> where
| ^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `B`
|
help: consider further restricting type parameter `B`
|
9 | (A, B, C): Default, B: std::default::Default
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `C: std::default::Default` is not satisfied
--> src/lib.rs:8:15
|
2 | struct Test<A: Default, B: Default, C: Default> {
| ------- required by this bound in `Test`
...
8 | impl<A, B, C> Test<A, B, C> where
| ^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `C`
|
help: consider further restricting type parameter `C`
|
9 | (A, B, C): Default, C: std::default::Default
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
In the end, the only thing that would compile was this:
impl<A, B, C> Test<A, B, C> where
A: Default,
B: Default,
C: Default
{ }
Is there a way to achieve this without having to write out the constraints in every impl?