我似乎找不到从HashSet. 受其他代码示例的启发,我写了以下内容:
my_set.iter().next().map(|i| my_set.take(i).unwrap())
Run Code Online (Sandbox Code Playgroud)
即获取集合值的迭代器,从中获取第一个值(引用),然后my_set.take()使用先前获得的引用运行,以获取值本身(而不是引用)并将其从集合中删除。
由于以下原因无法编译:
error[E0500]: closure requires unique access to `my_set` but it is already borrowed
|
32 | my_set.iter().next().map(|i| my_set.take(i).unwrap())
| ------ --- ^^^ ------ second borrow occurs due to use of `my_set` in closure
| | | |
| | | closure construction occurs here
| | first borrow later used by call
| borrow occurs here
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多很多变体,但它们都失败了,因为不可变的借用然后被借为可变的(错误 502)。
任何人都可以推荐一种重写上述内容的方法吗?