我目前有两个类似的 struct ,我需要实现From
特征来将一个转换为另一个.但是,我收到一个错误,内容是:
error[E0382]: borrow of moved value: `item.my_first_string`
--> src/lib.rs:14:30
|
13 | my_second_string: item.my_first_string,
| -------------------- value moved here
14 | string_is_empty: item.my_first_string.is_empty(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
|
= note: move occurs because `item.my_first_string` has type `String`, which does not implement the `Copy` trait
下面是我的代码:
struct Foo {
my_first_string: String,
}
struct Bar {
my_second_string: String,
string_is_empty: bool,
}
impl From<Foo> for Bar {
fn from(item: Foo) -> Self {
Bar {
my_second_string: item.my_first_string,
string_is_empty: item.my_first_string.is_empty(),
}
}
}
我知道我可以拨打my_first_string
的.clone
来满足借阅判断员的要求,但这似乎是不必要的.有没有办法在没有.clone()
的情况下使用这个字段两次?