在匹配声明和结果中相互借用

时间:2018-03-13 03:32:25

标签: rust borrow-checker

我试图确定某个容器是否有一个对象,如果有,则返回找到的对象,如果没有,则添加它。

我找到Rust borrow mutable self inside match expression 有一个答案说明我想要做的事情可以做到(无法做到)。

在我的情况下,我有一些带有孩子矢量的物体。我不想暴露我的对象的内部,因为我可能想要更改下面的表示。

{p> How can you resolve the need to mutably borrow in different match arms in Rust?似乎暗示如果我的生命时间正确,我可以做我想做的事,但我还没有弄清楚如何。

以下是我遇到的问题的代表:

fn find_val<'a>(container: &'a mut Vec<i32>, to_find: i32) -> Option<&'a mut i32> {
    for item in container.iter_mut() {
        if *item == to_find {
            return Some(item);
        }
    }

    None
}

fn main() {
    let mut container = Vec::<i32>::new();
    container.push(1);
    container.push(2);
    container.push(3);

    let to_find = 4;

    match find_val(&mut container, to_find) {
        Some(x) => {
            println!("Found {}", x);
        }
        _ => {
            container.push(to_find);
            println!("Added {}", to_find);
        }
    }
}

playground

我得到的错误是:

error[E0499]: cannot borrow `container` as mutable more than once at a time
  --> src/main.rs:24:13
   |
19 |     match find_val(&mut container, to_find) {
   |                         --------- first mutable borrow occurs here
...
24 |             container.push(to_find);
   |             ^^^^^^^^^ second mutable borrow occurs here
...
27 |     }
   |     - first borrow ends here

1 个答案:

答案 0 :(得分:2)

将更改放在函数中,并使用提前返回而不是else分支:

fn find_val_or_insert(container: &mut Vec<i32>, to_find: i32) {
    if let Some(x) = find_val(&container, to_find) {
        println!("Found {}", x);
        return; // <- return here instead of an else branch
    }
    container.push(to_find);
    println!("Added {}", to_find);
}

另请参阅Mutable borrow more than onceHow to update-or-insert on a Vec?