When I wrote the following code, some confusing compile errors occurred. Can anyone tell me how to deal with it?
Code:
Stage::Graduated(vision) => {
if vision.show_panel {
let mut tree = vec![];
for _ in vision.images.iter() {
tree.push(iced_native::widget::tree::Tree::empty());
}
let mut current_tree;
let mut points = iced_native::overlay::Group::new();
for (index, _i) in vision.images.iter().enumerate() {
current_tree = &mut tree[index];
let pinpoint = crate::button_from_svg();
let floating_element = FloatingElementOverlay::new(
&mut current_tree,
pinpoint,
&Anchor::SouthEast,
&Offset { x: 0.0, y: 0.0 },
);
points = points.push(floating_element.overlay(Point { x: 0.0, y: 0.0 }));
}
return column![points].into();
} else {
return column![].into();
}
}
Errors:
PS D:\graduate> cargo run
Compiling graduate v0.1.0 (C:\Users\amazi\graduate)
error[E0506]: cannot assign to `current_tree` because it is borrowed
--> src\main.rs:1172:33
|
1172 | ... current_tree = &mut tree[index];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `current_tree` is assigned to here but it was already borrowed
...
1180 | ... &mut current_tree,
| ----------------- `current_tree` is borrowed here
...
1185 | ... points = points.push(overlay.overlay(Point { x: 0.0, y: 0.0 }));
| ------ borrow later used here
error[E0499]: cannot borrow `tree` as mutable more than once at a time
--> src\main.rs:1172:53
|
1172 | current_tree = &mut tree[index];
| ^^^^ `tree` was mutably borrowed here in the previous iteration of the loop
...
1206 | } else {
| - first borrow might be used here, when `points` is dropped and runs the destructor for type `Group<'_, Message, iced_graphics::renderer::Renderer<iced_wgpu::backend::Backend, Theme>>`
Some errors have detailed explanations: E0499, E0506.
For more information about an error, try `rustc --explain E0499`.
error: could not compile `graduate` due to 2 previous errors