我有一个结构NotificationOption
和另一个结构NotificationOption2
以及一个实现From<NotificationOption>
for NotificationOption2
.
我想转换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();
但我收到编译器错误:
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>`
事实并非如此,但自己实现起来很简单:
let options2: Vec<NotificationOption2> = options.into_iter().map(|x| x.into()).collect();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)