有人能解释一下为什么它说它*profession是一个单位类型而它是一个向量吗?
use hashbrown::HashMap;
fn main() {
let mut sphere: HashMap<String, Vec<&str>> = HashMap::new();
sphere.insert(String::from("junior"), vec![]);
sphere.insert(String::from("Middle"), vec![]);
sphere.insert(String::from("Senior"), vec![]);
loop {
println!();
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.expect("What the hell this doesn't work!?");
if input.trim() == "stop" {
break;
}
let splited_data: Vec<&str> = input.split(" to ").collect();
let person = splited_data[0];
let category = splited_data[1].to_string();
let professions = sphere.entry(category.to_string()).or_insert(vec![]);
*professions.push(person);
}
}
Run Code Online (Sandbox Code Playgroud)
error[E0614]: type `()` cannot be dereferenced
--> src/lib.rs:21:9
|
21 | *professions.push(person);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
该*引用操作具有优先级低于.,所以这个:
*professions.push(person);
Run Code Online (Sandbox Code Playgroud)
相当于:
*(professions.push(person));
Run Code Online (Sandbox Code Playgroud)
您看到的错误是因为Vec::push返回().
您真正想要的是取消引用向量,然后调用push:
(*professions).push(person);
Run Code Online (Sandbox Code Playgroud)
但是 Rust 的自动解引用规则使显式解引用变得不必要,你可以只写:
professions.push(person);
Run Code Online (Sandbox Code Playgroud)
另请参阅:Rust 的确切自动解引用规则是什么?
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |