Cloning of the structure includes `Rc <Fn (T)>`?
I want to determine the type, include Rc<Fn(T)>
, T
is an optional Clone
attribute, code example:
use std::rc::Rc; struct X; #[derive(Clone)] struct Test<T> { a: Rc<Fn(T)> } fn main() { let t: Test<X> = Test { a: Rc::new(|x| {}) }; let a = t.clone(); }
cannot be executed, error message:
test.rs:16:15: 16:22 note: the method `clone` exists but the following trait bounds were not satisfied: `X : core::clone::Clone`, `X : core::clone::Clone` test.rs:16:15: 16:22 help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `clone`, perhaps you need to implement it: test.rs:16:15: 16:22 help: candidate #1: `core::clone::Clone` error: aborting due to previous error
How to fix my code?
+5
1 answer
The problem is that #[derive(Clone)]
pretty stupid. As part of his extension, he adds a Clone
constraint to all parameters of the type type, regardless of whether it really needs such a constraint.
Thus, you need to implement Clone
manually, for example:
struct Test<T> { a: Rc<Fn(T)> } impl<T> Clone for Test<T> { fn clone(&self) -> Self { Test { a: self.a.clone(), } } }
+3