小编chr*_*h-k的帖子

如何将 HashMap 传递给 Rust 中的函数

谁能解释为什么下面的代码不能编译?

use std::collections::HashMap;

fn add(mut h: &HashMap<&str, &str>) {
    h.insert("foo", "bar");
}

fn main() {
    let mut h: HashMap<&str, &str> = HashMap::new();
    add(&h);
    println!("{:?}", h.get("foo"));
}
Run Code Online (Sandbox Code Playgroud)

这就是 rustc 告诉我的

hashtest.rs:4:5: 4:6 error: cannot borrow immutable borrowed content `*h` as mutable
hashtest.rs:4     h.insert("foo", "bar");
                  ^
Run Code Online (Sandbox Code Playgroud)

hashmap rust

4
推荐指数
1
解决办法
3859
查看次数

标签 统计

hashmap ×1

rust ×1