从和进入实施
implementing from and into
我想将“A”的类型转换为“B”,并将“A”的集合转换为“B”的集合(反之亦然)。
我对机制的运作方式有些误解。
我假设在基本类型上实现 From
会类似地转移到集合,而无需显式实现。
例如:
struct A {
text: String
}
struct B {
text: String
}
impl From<A> for B {
fn from(a: A) -> Self {
B { text: a.text }
}
}
fn main() {
let a = A { text: "hello".to_string() };
let b = B::from(a); // works
let a2 = A { text: "hello".to_string() };
let b2 = a.into(); // works
let v1 = vec![A { text: "hello".to_string()}];
let v2 = Vec::<B>::from(v1); // doesn't work
let v2 : Vec<B> = v1.into(); // doesn't work
}
我在转换集合时遇到的错误:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Vec<B>: From<Vec<A>>` is not satisfied
--> src/main.rs:20:14
|
20 | let v2 = Vec::<B>::from(v1); // doesn't work
| ^^^^^^^^^^^^^^ the trait `From<Vec<A>>` is not implemented for `Vec<B>`
|
= help: the following implementations were found:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
and 6 others
= note: required by `from`
error[E0277]: the trait bound `Vec<B>: From<Vec<A>>` is not satisfied
--> src/main.rs:21:26
|
21 | let v2 : Vec<B> = v1.into(); // doesn't work
| ^^^^ the trait `From<Vec<A>>` is not implemented for `Vec<B>`
|
= help: the following implementations were found:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
and 6 others
= note: required because of the requirements on the impl of `Into<Vec<B>>` for `Vec<A>`
是否有针对这些嵌套转换的“一揽子”实施?如果不是,实现这种灵活性的最佳方法是什么?
在将项目收集到新向量之前使用 into_iter
and map Into::into
消耗 vec:
let v2: Vec<B> = v1.into_iter().map(Into::into).collect();
如果您正在寻找与 From::from
and Into::into
提供的语法类似的语法,您可能需要考虑编写自己的通用特征,FromVec
,例如:
trait FromVec<T> {
fn from_vec(val: Vec<T>) -> Self;
}
impl<T, S: From<T>> FromVec<T> for Vec<S> {
fn from_vec(val: Vec<T>) -> Self {
val.into_iter().map(Into::into).collect()
}
}
连同其相应的伙伴特征,IntoVec
:
trait IntoVec<T> {
fn into_vec(self) -> Vec<T>;
}
并为其提供一揽子实施:
impl<T, S> IntoVec<T> for Vec<S>
where Vec<T>: FromVec<S>
{
fn into_vec(self) -> Vec<T> {
Vec::from_vec(self)
}
}
这样,您将以与调用 from()
和 into()
类似的方式调用 from_vec()
和 into_vec()
:
fn main() {
let vec_a = vec![A { text: "hello".to_string() } ];
let vec_b = Vec::<B>::from_vec(vec_a);
let vec_a = vec![A { text: "hello".to_string() } ];
let vec_b: Vec<B> = vec_a.into_vec();
}
我想将“A”的类型转换为“B”,并将“A”的集合转换为“B”的集合(反之亦然)。
我对机制的运作方式有些误解。
我假设在基本类型上实现 From
会类似地转移到集合,而无需显式实现。
例如:
struct A {
text: String
}
struct B {
text: String
}
impl From<A> for B {
fn from(a: A) -> Self {
B { text: a.text }
}
}
fn main() {
let a = A { text: "hello".to_string() };
let b = B::from(a); // works
let a2 = A { text: "hello".to_string() };
let b2 = a.into(); // works
let v1 = vec![A { text: "hello".to_string()}];
let v2 = Vec::<B>::from(v1); // doesn't work
let v2 : Vec<B> = v1.into(); // doesn't work
}
我在转换集合时遇到的错误:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Vec<B>: From<Vec<A>>` is not satisfied
--> src/main.rs:20:14
|
20 | let v2 = Vec::<B>::from(v1); // doesn't work
| ^^^^^^^^^^^^^^ the trait `From<Vec<A>>` is not implemented for `Vec<B>`
|
= help: the following implementations were found:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
and 6 others
= note: required by `from`
error[E0277]: the trait bound `Vec<B>: From<Vec<A>>` is not satisfied
--> src/main.rs:21:26
|
21 | let v2 : Vec<B> = v1.into(); // doesn't work
| ^^^^ the trait `From<Vec<A>>` is not implemented for `Vec<B>`
|
= help: the following implementations were found:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
and 6 others
= note: required because of the requirements on the impl of `Into<Vec<B>>` for `Vec<A>`
是否有针对这些嵌套转换的“一揽子”实施?如果不是,实现这种灵活性的最佳方法是什么?
在将项目收集到新向量之前使用 into_iter
and map Into::into
消耗 vec:
let v2: Vec<B> = v1.into_iter().map(Into::into).collect();
如果您正在寻找与 From::from
and Into::into
提供的语法类似的语法,您可能需要考虑编写自己的通用特征,FromVec
,例如:
trait FromVec<T> {
fn from_vec(val: Vec<T>) -> Self;
}
impl<T, S: From<T>> FromVec<T> for Vec<S> {
fn from_vec(val: Vec<T>) -> Self {
val.into_iter().map(Into::into).collect()
}
}
连同其相应的伙伴特征,IntoVec
:
trait IntoVec<T> {
fn into_vec(self) -> Vec<T>;
}
并为其提供一揽子实施:
impl<T, S> IntoVec<T> for Vec<S>
where Vec<T>: FromVec<S>
{
fn into_vec(self) -> Vec<T> {
Vec::from_vec(self)
}
}
这样,您将以与调用 from()
和 into()
类似的方式调用 from_vec()
和 into_vec()
:
fn main() {
let vec_a = vec![A { text: "hello".to_string() } ];
let vec_b = Vec::<B>::from_vec(vec_a);
let vec_a = vec![A { text: "hello".to_string() } ];
let vec_b: Vec<B> = vec_a.into_vec();
}