B. *_*ang 2 hashmap rust borrowing
该程序接受整数N,后跟N行,包含由空格分隔的两个字符串.我想将这些行放入HashMap使用第一个字符串作为键,第二个字符串作为值:
use std::collections::HashMap;
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("unable to read line");
let desc_num: u32 = match input.trim().parse() {
Ok(num) => num,
Err(_) => panic!("unable to parse")
};
let mut map = HashMap::<&str, &str>::new();
for _ in 0..desc_num {
input.clear();
io::stdin().read_line(&mut input)
.expect("unable to read line");
let data = input.split_whitespace().collect::<Vec<&str>>();
println!("{:?}", data);
// map.insert(data[0], data[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
该计划按预期工作:
3
a 1
["a", "1"]
b 2
["b", "2"]
c 3
["c", "3"]
Run Code Online (Sandbox Code Playgroud)
当我尝试将这些已解析的字符串放入HashMap并取消注释时map.insert(data[0], data[1]);,编译失败并显示以下错误:
error: cannot borrow `input` as mutable because it is also borrowed as immutable [E0502]
input.clear();
^~~~~
note: previous borrow of `input` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `input` until the borrow ends
let data = input.split_whitespace().collect::<Vec<&str>>();
^~~~~
note: previous borrow ends here
fn main() {
...
}
^
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会出现这个错误,因为我认为map.insert()表达式根本没有借用字符串input.
小智 6
split_whitespace()没有给你两个新的Strings包含(副本)输入的非空白部分.相反,你得到两个引用到由input类型管理的内存&str.因此,当您尝试清除input并读取下一行输入时,您将尝试覆盖仍由哈希映射使用的内存.
为什么split_whitespace(和我应该添加的许多其他字符串方法)通过返回使问题复杂化&str?因为它经常就足够了,在这种情况下它可以避免不必要的副本.但是,在这种特定情况下,最好显式复制字符串的相关部分:
map.insert(data[0].clone(), data[1].clone());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |