相关疑难解决方法(0)

如果/ else阻塞,如何在迭代器中使用String而不是&str

有人会友好地向我解释为什么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

3
推荐指数
1
解决办法
135
查看次数

标签 统计

rust ×1