I have a struct similar to this
#[derive(Debug, Clone)]
pub struct Token {
pub lexeme: String,
pub value: Option<Box<dyn Any>>,
pub line: u32,
}
Adding the #[derive(Clone)]
part leads to the compilation error:
error[E0277]: the trait bound `dyn std::any::Any: Clone` is not satisfied
--> src/token.rs:85:5
|
81 | #[derive(Debug, Clone)]
| ----- in this derive macro expansion
...
85 | pub value: Option<Box<dyn Any>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `dyn std::any::Any`
|
= note: required for `Box<dyn std::any::Any>` to implement `Clone`
= note: 1 redundant requirement hidden
= note: required for `Option<Box<dyn std::any::Any>>` to implement `Clone`
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
It seems the issue is Any
does not implement Clone
which kinda makes sense since Any
means any type.
The question now is, with such a data structure, how would one go about using it in cases where it values needs to be clones or copied?