相关疑难解决方法(0)

从HashMap或Vec返回引用会导致借用超出其所在的范围?

我有一个持久的编译错误,其中Rust抱怨我在尝试可变借用时有一个不可变的借位,但是不可变借用来自另一个范围,而我并没有从中带来任何东西.

我有一些代码检查地图中的值,如果它存在,则返回它,否则它需要以各种方式改变地图.问题是我似乎无法找到让Rust同时执行的方法,即使这两个操作完全分开.

这是一些荒谬的代码,它遵循与我的代码相同的结构并展示了问题:

use std::collections::BTreeMap;

fn do_stuff(map: &mut BTreeMap<i32, i32>, key: i32) -> Option<&i32> {
    // extra scope in vain attempt to contain the borrow
    {
        // borrow immutably
        if let Some(key) = map.get(&key) {
            return Some(key);
        }
    }

    // now I'm DONE with the immutable borrow, but rustc still thinks it's borrowed

    map.insert(0, 0); // borrow mutably, which errors
    None
}
Run Code Online (Sandbox Code Playgroud)

这出错了:

error[E0502]: cannot borrow `*map` as mutable because it is also borrowed as immutable
  --> src/lib.rs:14:5
   |
3 …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

16
推荐指数
1
解决办法
1836
查看次数

如何修改数组的最后一项?

由于arr被借为可变的,arr因此无法通过调用获得长度len()。我被困在这里,正确的方法是什么?

fn double_last(arr: &mut[i32]) -> &i32 {
    let last = &mut arr[arr.len() - 1];  // borrow checker error.
    //let last = &mut arr[3];            // fine
    *last *= 2;
    last
}

fn main() {
    let mut a = [1,2,3,4];
    println!("{}", double_last(&mut a));
    println!("{:?}", a);
}
Run Code Online (Sandbox Code Playgroud)

arrays rust

5
推荐指数
2
解决办法
1333
查看次数

如何在Vec上更新或插入?

我在Rust写一个数据结构.它包含一Vec对键值对.当插入到结构中时,我需要找到匹配的键并更新键和值(实际上是子指针).代码看起来有点像这样,其中pivots是一个ref mutto Vec<Pivot>,Pivot它只是一个包含两个字段的结构:

match pivots.iter_mut().find(|ref p| key <= p.min_key) { // first mutable borrow
    Some(ref mut pivot) => {
        // If there is one, insert into it and update the pivot key
        pivot.min_key = key;
        pivot.child.insert(key, value) // recursive call
    },
    // o/w, insert a new leaf at the end
    None => pivots.push(Pivot /* ... */) // second mutable borrow
}
Run Code Online (Sandbox Code Playgroud)

但是有一个问题.即使我没有在第二部分中使用可变迭代器match,借用检查器也会抱怨我"不能*pivots一次多次借用可变的迭代".

这对我来说是完全合理的,因为第一次借用仍然在范围内,即使它没有在那种情况下使用match.这有点不方便:一个聪明的检查员当然可以说借用是不重叠的.我见过有人在线建议使用早期返回以避免问题,如下所示:

match pivots.iter_mut().find(|ref …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

5
推荐指数
1
解决办法
1154
查看次数

标签 统计

rust ×3

borrow-checker ×2

arrays ×1