作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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>
类型?
最佳答案
你的情况可以减少到
fn repro(_: String) {}
fn main() {
repro(*"".to_string());
}
出于某种原因,您正在取消引用 String
.字符串实现 Deref<Target = str>
, 所以你已经明确地尝试制作一个 str
.这最终是行不通的,因为 str
未调整大小,但您首先遇到类型不匹配。
去掉星号:
fn repro(_: String) {}
fn main() {
repro("".to_string());
}
另见:
关于rust - 预期类型 String,当我明确将其设为 String 时发现类型 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56497357/
由“CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", ES_MULTILINE.."创建的文本框需要\r\n 换行。我将我的 stdoutput 重定向到那个文
我是一名优秀的程序员,十分优秀!