【发布时间】:2020-12-16 18:23:09
【问题描述】:
我有一个结构 NotificationOption 和另一个结构 NotificationOption2。我有一个From<NotificationOption> 的实现NotificationOption2。
我也在尝试将Vec<NotificationOption> 转换为Vec<NotificationOption2>。我得到一个编译器错误:
#[derive(Clone)]
pub struct NotificationOption {
pub key: String,
}
#[derive(Serialize, Deserialize)]
pub struct NotificationOption2 {
pub key: String,
}
impl From<NotificationOption> for NotificationOption2 {
fn from(n: NotificationOption) -> Self {
Self {
key: n.key,
}
}
}
let options : Vec<NotificationOption> = Vec::new();
/// add some options...
// some other point in code
let options2: Vec<NotificationOption2> = options.into();
| ^^^^ the trait `std::convert::From<std::vec::Vec<NotificationOption>>` is not implemented for `std::vec::Vec<NotificationOption2>`
【问题讨论】:
标签: generics vector rust traits parametric-polymorphism