如果这很简单,我向你道歉。我正在学习Rust,并逐渐适应奇怪的借用系统。通常,通过改变方法调用的语法就可以获得所需的行为,但是,在这种情况下,似乎没有办法。
我的代码简化版本如下:EventPump
来自 SDL。
struct Example {
pump: EventPump
}
impl Example {
fn method(&mut self) {
for event in pump.poll_iter() {
self.other_method();
}
}
fn other_method(&self) {
}
}
然而,我遇到了以下错误:
error[E0502]: cannot borrow `*self` as immutable because it is also borrowed as mutable
--> src\game.rs:53:67
|
30 | for event in self.pump.poll_iter();
| ---------------------
| |
| mutable borrow occurs here
| mutable borrow later used here
...
53 | self.other_method();
| ^^^^ immutable borrow occurs here
也许有一种正确的方法可以让我的结构体保持自身所有权,但我没有找到它。
我尝试过以下方法:
- 将其转换为带有显式
while let event = iterator.next()
的 while 循环,仍然出现错误 - 使函数变为可变函数,错误消息现在说不允许两个可变引用。我猜错误消息中的整个"不可变性"实际上是无关紧要的。
我可能可以将可迭代对象的整个内容复制到向量或类似的数据类型中,但这会破坏迭代器的目的,而且如果迭代器不是有限的怎么办?肯定有更好的方法吧...
如果有更多 Rust 经验的人能帮我解决问题,将不胜感激。
self.event_pump.as_ref().unwrap().as_ptr().as_ref().unwrap().keyboard_state()
,这也是不安全的,所以并不理想。 - mousetail