解决嵌套可变借用conflct的惯用方法

Vit*_*meo 2 rust borrow-checker

考虑下面的最小示例,它提供了我在一些实际代码中遇到的情况:

use std::collections::HashSet;
type HS = HashSet<String>;

fn fn1(x: String, hs0: &mut HS, hs1: &mut HS) {
    // ...
}

fn fn0(hs0: &mut HS, hs1: &mut HS) {
    hs0.get("").map(|x| fn1(x.clone(), hs0, hs1));
}

fn main() {
    let mut hs0 = HS::new();
    let mut hs1 = HS::new();
    fn0(&mut hs0, &mut hs1);
}
Run Code Online (Sandbox Code Playgroud)

借款检查员不满意:

error[E0500]: closure requires unique access to `hs0` but `*hs0` is already borrowed
 --> <anon>:9:21
  |
9 |     hs0.get("").map(|x| fn1(x, hs0, hs1));
  |     ---             ^^^        ---      - borrow ends here
  |     |               |          |
  |     |               |          borrow occurs due to use of `hs0` in closure
  |     |               closure construction occurs here
  |     borrow occurs here
Run Code Online (Sandbox Code Playgroud)

在Rust操场上


我理解上面的错误信息,我想知道解决这个问题的惯用方法.注意:

  • 出于可读性/可测试性的原因,我希望fn0fn1成为单独的函数.(即他们自己独自理解.)

  • 我想打电话fn1fn0与在相同的参数.map(...)链式调用.

唯一明智的选择是不使用.map

red*_*ime 5

粗略地说,hs0仍然借用,直到没有任何参考.也就是说,在x: &String生活中,你不能随意借钱hs0.这意味着我们需要做一些事情来结束其生命周期x,比如将其转换为String并将该字符串传递给下一个字符串map.

fn fn0(hs0: &mut HS, hs1: &mut HS) {
    hs0.get("").map(|x| x.clone()).map(|x| fn1(x, hs0, hs1));
}
Run Code Online (Sandbox Code Playgroud)