Mutable borrow in loop considered "more than once at a time"?

โš“ Rust    ๐Ÿ“… 2026-03-05    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 4      

surdeus

I 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

Read full topic

๐Ÿท๏ธ Rust_feed