I have a struct NotificationOption and another struct NotificationOption2 as well as an implementation for From<NotificationOption> for NotificationOption2.
I'm would like to convert Vec<NotificationOption> to a Vec<NotificationOption2>:
struct NotificationOption {
key: String,
}
struct NotificationOption2 {
key: String,
}
impl From<NotificationOption> for NotificationOption2 {
fn from(n: NotificationOption) -> Self {
Self {
key: n.key,
}
}
}
let options: Vec<NotificationOption> = ...;
let options2: Vec<NotificationOption2> = options.into();
But I get a compiler error:
error[E0277]: the trait bound `Vec<NotificationOption2>: From<Vec<NotificationOption>>` is not satisfied
--> src/main.rs:22:46
|
22 | let options2: Vec<NotificationOption2> = options.into();
| ^^^^^^^ ---- required by a bound introduced by this call
| |
| the trait `From<Vec<NotificationOption>>` is not implemented for `Vec<NotificationOption2>`
Seems not, and it makes sense - you assume that the conversion from
Vec<NotificationOption>toVec<NotificationOption2>is to create aVec<NotificationOption2>, convert theNotificationOptionitems intoNotificationOption2using.into()and add them to the vector. Rust has no reason to assume this is the conversion and not some other routine.You also can't implement
Fromgenerically for a Vec to Vec since your crate didn't defineFrom. You can perhaps create a conversion trait of your own, and implement it generically betweenVec<X: Into<Y>>andVec<Y>.