use std::collections::{HashMap, HashSet};
type Snapshot = HashMap<String, HashSet<String>>;
fn compare_snapshots(snapshot1: Snapshot, snapshot2: Snapshot, entry1: String, entry2: String) {}
fn main() {
let snapshot1 = HashMap::new();
let snapshot2 = HashMap::new();
let entries1: HashSet<String> = snapshot1.keys().cloned().collect();
let entries2: HashSet<String> = snapshot2.keys().cloned().collect();
let entries: Vec<String> = entries1.union(&entries2).cloned().collect();
for e1 in entries {
for e2 in entries {
if e1 < e2 {
compare_snapshots(snapshot1, snapshot2, *e1.to_string(), *e2.to_string());
}
}
}
}
以及以下错误:
error[E0308]: mismatched types
--> src/main.rs:18:57
|
18 | compare_snapshots(snapshot1, snapshot2, *e1.to_string(), *e2.to_string());
| ^^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
|
= note: expected type `std::string::String`
found type `str`
error[E0308]: mismatched types
--> src/main.rs:18:74
|
18 | compare_snapshots(snapshot1, snapshot2, *e1.to_string(), *e2.to_string());
| ^^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
|
= note: expected type `std::string::String`
found type `str`
鉴于我明确赋予entries
一种Vec<String>
类型的信息,为什么我得到它们?
答案 0 :(得分:1)
您的情况可以减少为
fn repro(_: String) {}
fn main() {
repro(*"".to_string());
}
由于某种原因,您正在取消引用String
。字符串实现了Deref<Target = str>
,因此您已明确尝试制作一个str
。最终将无法正常工作,因为str
的大小不正确,但是您首先遇到的类型不匹配。
删除星号:
fn repro(_: String) {}
fn main() {
repro("".to_string());
}
另请参阅: