有人会友好地向我解释为什么String在这个脚本中使用a 不起作用,但&str确实如此.另外,我如何修改它以便它可以用于String?[版本1.2]
use std::collections::{HashMap};
fn main() {
let mut hash = HashMap::<&str, &str>::new();
hash.insert("this", "value");
let l: &str = "this is a borrowed string reference";
// If the above line was defined as:
//let l: String = "this is a string".to_string();
let mut all = l.split(" ");
let name: &str = all.next().unwrap();
if hash.contains_key(name) == true {
hash.remove(name);
} else {
hash.insert(name, "stuff");
}
}
Run Code Online (Sandbox Code Playgroud) rust ×1