我编写了以下工作代码:
#[derive(Debug, PartialEq, Serialize)]
struct Grid64Row {
...
cell: String,
...
}
...
let mut rows_by_cell: HashMap::<String, Vec<Grid64Row>> = HashMap::new();
for row in rows {
match rows_by_cell.entry(row.cell.clone()) {
Entry::Vacant(e) => { e.insert(vec![row]); },
Entry::Occupied(mut e) => { e.get_mut().push(row); }
}
}
据我所知,我需要row.cell
-String
的clone()
次.编译器同意,如果我删除.clone()
,我会得到:
error[E0382]: use of partially moved value: `row`
--> foobar/src/main.rs:97:49
|
96 | match rows_by_cell.entry(row.cell) {
| -------- value partially moved here
97 | Entry::Vacant(e) => { e.insert(vec![row]); },
| ^^^ value used here after partial move
|
= note: partial move occurs because `row.cell` has type `std::string::String`, which does not implement the `Copy` trait
有没有办法解决这个短命的克隆?(我不明白为什么‘.entry()’需要String
分,而不是&str
分,但即使这样也会有借款问题.)