Mutable borrow in loop considered "more than once at a time"?
โ Rust ๐ 2026-03-05 ๐ค surdeus ๐๏ธ 4I have a function that looks something like this (I cut out some irrelevant parts). Itโs doing a mutable borrow inside a loop.
fn touches_began(&mut self, event: UIEvent) {
let mut wctx = WindowCtx::new(&mut self.ctx_state);
let te = event.touches_enumerator();
while let Some(obj) = te.next_object() {
let pointer_id: u64 = ...;
let mut pevent = PointerEvent::new(pointer_id, &mut wctx);
pointer_down(&mut self.state, &mut pevent);
}
self.ui_loop_end();
}
The compiler error:
error[E0499]: cannot borrow `wctx` as mutable more than once at a time
--> bambu-ui/src/os_spec/apple/ios/windows.rs:165:93
|
165 | let mut pevent = PointerEvent::new(pointer_id, &mut wctx);
| ^^^^^^^^^
| |
| `wctx` was mutably borrowed here in the previous iteration of the loop
| first borrow used here, in later iteration of loop
It seems like it is not borrowed โmore than once at a timeโ. The event borrows it, but is destroyed within each iteration. Why does it think there is a problem?
9 posts - 3 participants
๐ท๏ธ Rust_feed