【发布时间】:2018-03-01 22:14:47
【问题描述】:
我有点疑惑为什么在第二种情况下取消引用 &&str 似乎不起作用:
use std::collections::HashSet;
fn main() {
let days = vec!["mon", "tue", "wed"];
let mut hs: HashSet<String> = HashSet::new();
for d in &days {
// works
hs.insert(String::from(*d));
// doesn't
hs.insert(*d.to_string());
}
println!("{:#?}", hs);
}
str 确实实现了 ToString 特征,但它仍然给我错误:
error[E0308]: mismatched types
--> src/main.rs:12:19
|
12 | hs.insert(*d.to_string());
| ^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
|
= note: expected type `std::string::String`
found type `str`
我在这里弄错了什么语法?
【问题讨论】:
-
注意函数调用的优先级高于deref;
*d.to_string()将取消引用应用于调用结果。
标签: rust