#Cannot borrow as mutable more than once at a time

13 messages · Page 1 of 1 (latest)

brisk sapphire
#

I'm trying to add a delete button to a row in a table but it doesn't like it.. What can I do?

for (index, process) in self.data.processes.iter_mut().enumerate() {
    body.row(30., |mut row| {
        row.col(|ui| {
            ui.checkbox(&mut process.enabled, "");
        });
        row.col(|ui| {
            ui.label(&process.name);
        });
        row.col(|ui| {
            ui.checkbox(&mut process.restore, "");
        });
        row.col(|ui| {
            ui.checkbox(&mut process.admin, "");
        });
        row.col(|ui| {
            if ui.button("✎").clicked() {
                self.editing = Some(process.clone());
                self.editing_index = Some(index as u8);
            };
            if ui.button("🗑").clicked() {
                self.data.processes.remove(index);
            };
        });
    });
}```
#

I guess I could fix this with a confirmation dialog but I don't want that

#

is there something like process.nextTick() from NodeJS? (would have same issue lol)

storm bramble
#

Please share the error message

brisk sapphire
#

right sry

#
error[E0499]: cannot borrow `*self.data.processes` as mutable more than once at a time
   --> src\main.rs:156:51
    |
155 | ...                   for (index, process) in self.data.processes.iter_mut().enumerate() {
    |                                               ------------------------------------------
    |                                               |
    |                                               first mutable borrow occurs here
    |                                               first borrow later used here
156 | ...                       body.row(30., |mut row| {
    |                                         ^^^^^^^^^ second mutable borrow occurs here
...
175 | ...                                   self.data.processes.remove(index);
    |                                       ------------------- second borrow occurs due to use of `*self.data.processes` in closure

sour mauve
#

Looks like you want retain

storm bramble
#

retain_mut probably, but yeah

brisk sapphire
#

what about the index?

#

i need it when editing

sour mauve
#

You can capture it:

let mut index = 0;
self.data.processes.retain_mut(|item| {
  // do stuff
  index += 1;
});
#

Not really elegant but works

brisk sapphire
#

Okey thanks