gpt4 book ai didi

rust - Rust 引用书中的问题

转载 作者:行者123 更新时间:2023-12-05 02:27:58 28 4
gpt4 key购买 nike

https://doc.rust-lang.org/reference/types/closure.html#capture-modes

struct SetVec {
set: HashSet<u32>,
vec: Vec<u32>
}

impl SetVec {
fn populate(&mut self) {
let vec = &mut self.vec;
self.set.iter().for_each(|&n| {
vec.push(n);
})
}
}

If, instead, the closure were to use self.vec directly, then it wouldattempt to capture self by mutable reference. But since self.set isalready borrowed to iterate over, the code would not compile.

引用书上说代码无法编译,但是代码可以编译。那为什么?

最佳答案

这在过去是正确的,但此后发生了变化,并且引用未更新。自 2021 版以来,closures capture disjoint fields .

参见 reference issue #1066 .

关于rust - Rust 引用书中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72784724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com