简体   繁体   English

Rust用整个HashMap查借用,不查key,有什么好办法吗?

[英]Rust check borrow with the whole HashMap, not check the key, is there any good way?

I want move the elements of HashMap<u64, Vec> key=1 to key=2我想将 HashMap<u64, Vec> key=1 的元素移动到 key=2

use std::collections::HashMap;

fn main() {
    let mut arr: HashMap<u64, Vec<u64>> = HashMap::new();
    arr.insert(1, vec![10, 11, 12]); // in fact, elments more than 1,000,000, so can't use clone()
    arr.insert(2, vec![20, 21, 22]);

    // in fact, following operator is in recusive closure, I simplify the expression:
    let mut vec1 = arr.get_mut(&1).unwrap();
    let mut vec2 = arr.get_mut(&2).unwrap();

    // move the elements of key=1 to key=2
    for v in vec1 {
        vec2.push(vec1.pop().unwrap());
    }
}

got error:得到错误:

error[E0499]: cannot borrow `arr` as mutable more than once at a time
  --> src/main.rs:10:20
   |
9  |     let mut vec1 = arr.get_mut(&1).unwrap();
   |                    --- first mutable borrow occurs here
10 |     let mut vec2 = arr.get_mut(&2).unwrap();
   |                    ^^^ second mutable borrow occurs here
11 |     for v in vec1 {
   |              ---- first borrow later used here

Rust check borrow with the whole HashMap, not check the key. Rust 检查借用整个 HashMap,而不是检查键。 Is there any good way ?有什么好办法吗?

It's not clear what the context / constraints are, so depending on those there are various possibilities of different impact and levels of complexity不清楚上下文/约束是什么,因此根据这些,存在不同影响和复杂程度的各种可能性

While the third option has to traverse the entire hashmap (which is less efficient than directly finding the correct entries by hashing), it has the possible advantage of not "losing" v1's allocation which is useful if v1 will be filled again in the future: in the first option v1 is completely dropped, and in the second option v1 becomes a vector of capacity 0 (unless you swap in a vector with a predefined capacity, but that's still an extra allocation)虽然第三个选项必须遍历整个 hashmap(这比直接通过散列找到正确的条目效率低),但它具有不“丢失”v1 的分配的可能优势,如果 v1 将来再次填充,这很有用:在第一个选项中 v1 被完全删除,在第二个选项中 v1 成为容量为 0 的向量(除非您交换具有预定义容量的向量,但这仍然是额外的分配)

You can put the Vec into a RefCell moving the borrow check to runtime:您可以将Vec放入RefCell将借用检查移动到运行时:

use std::cell::RefCell;
use std::collections::HashMap;

fn main() {
    let mut arr: HashMap<u64, RefCell<Vec<u64>>> = HashMap::new();
    arr.insert(1, RefCell::new(vec![10, 11, 12])); // in fact, elments more than 1,000,000, so can't use clone()
    arr.insert(2, RefCell::new(vec![20, 21, 22]));

    // in fact, following operator is in recusive closure, I simplify the expression:
    let mut vec1 = arr.get(&1).unwrap().borrow_mut();
    let mut vec2 = arr.get(&2).unwrap().borrow_mut();

    // move the elements of key=1 to key=2
    vec2.append(&mut vec1);
}

Tip: Use Vec::append which moves the values from one vector to another.提示:使用Vec::append将值从一个向量移动到另一个向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 过度工程:在 Rust 中的 HashMap 键上使用参考(借用?) - Over Engineering: Using reference (borrow?) on HashMap key in Rust 如何在 Rust 中检查两个 HashMap 是否相同? - How to check two HashMap are identical in Rust? 有没有办法将无序对用作 rust Hashmap 密钥? - Is there a way to use an unordered pair as a rust Hashmap key? 如何检查 Rust 中当前是否按下了某个键? - How to check if a key is currently pressed in Rust? rust借位检查看起来很聪明,它可以检查和平面读写循环。 但我怎样才能绕过它? - rust borrow check looks very smart , it can check and flat reads and writes of loop. but how can I bypass it? 有什么方法可以在 Rust 中一次将多个条目插入到 HashMap 中? - Is there any way to insert multiple entries into a HashMap at once in Rust? (Rust) 有没有办法为重载的运算符自动借用? - (Rust) Is there a way to auto-borrow for overloaded operators? 检查字符串是否以 Rust 中的数字开头的更简单方法? - Simpler way to check if string start with a digit in Rust? Rust:为什么在struct内部借用引用会借用整个struct? - Rust: why does borrowing a reference inside struct borrow the whole struct? 将 Hashmap 点作为 Rust 中的参数传递是否有好主意? - Is there good idea to pass Hashmap Point as parameter in Rust?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM