在匹配语句和结果中可变借用

Mutably borrowing in match statement and result

我正在尝试确定容器是否有对象,return 如果有则找到找到的对象,如果没有则添加它。

我找到了 Rust borrow mutable self inside match expression 其中有一个答案说我正在尝试做的事情不能(不能?)完成。

在我的情况下,我有一些具有子向量的对象。我不想暴露对象的内部结构,因为我可能想更改下面的表示。

似乎暗示如果我的生命周期正确,我也许可以做我想做的事,但我一直无法弄清楚如何做。

以下是我遇到的问题的描述:

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

将更改放入函数中,并使用早期 return 而不是 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);
}

另见 and