The answer is buried in the error message:
= note: the method `clone` exists but the following trait bounds were not satisfied: `T: std::clone::Clone` which is required by `Foo<'_, T>: std::clone::Clone`
When you derive Clone
(and many other automatically-derived types), it adds a Clone
bound on all generic types. Using rustc -Z unstable-options --pretty=expanded
, we can see what it becomes:
impl <'a, T: ::std::clone::Clone + 'a> ::std::clone::Clone for Foo<'a, T> {
#[inline]
fn clone(&self) -> Foo<'a, T> {
match *self {
Foo { t: ref __self_0_0 } =>
Foo{t: ::std::clone::Clone::clone(&(*__self_0_0)),},
}
}
}
In this case, the bound is not needed because the generic type is behind a reference.
For now, you will need to implement Clone
yourself. There’s a Rust issue for this, but it’s a comparatively rare case with a workaround.