-
Consider this code, the error is `error[E0499]: cannot borrow self.state_hashmap as mutable more than once at a time fn new() -> Counter {
fn view(&mut self) -> Element {
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Each call to A possibility would be to use fn view<'a>(&'a mut self) -> Element<'a> { // There is no need to make lifetimes explicit but this is that your code will desugar to
let mut row = Row::new().padding(20).align_items(Align::Center);
let keys = [1, 2];
for (_, state) in self.state_map.iter_mut().filter(|(k, _)| keys.contains(k)) {
row = row.push(Button::new(state, Text::new("Increment")));
}
row.into()
} Keep in mind though that |
Beta Was this translation helpful? Give feedback.
-
An other possibility is to store your states in res.states_map.insert(1, RefCell::new(button::State::new()));
res.states_map.insert(2, RefCell::new(button::State::new()));
res.states_map.insert(3, RefCell::new(button::State::new()));
fn view(&mut self) -> Element {
Row::new()
.padding(20)
.align_items(Align::Center)
.push(
Button::new(self.state_hashmap.get(1).unwrap().borrow_mut(), Text::new("Increment"))
.on_press(Message::IncrementPressed),
)
.push(
Button::new(self.state_hashmap.get(&2).unwrap().borrow_mut(), Text::new("Increment"))
.on_press(Message::IncrementPressed),
)
.push(Text::new(self.value.to_string()).size(50))
.into()
} |
Beta Was this translation helpful? Give feedback.
-
Thx, looks like the question was more about rust itself rather than iced. |
Beta Was this translation helpful? Give feedback.
-
Why are you storing your states in a let states = [State::default(); 3];
states
.iter_mut()
.fold(Row::new(), |mut row, state| {
row.push(Button::new(state, content))
}) or let states = [State::default(); 3];
let mut row = Row::new();
for state in &mut states {
row = row.push(Button::new(state, content));
} |
Beta Was this translation helpful? Give feedback.
-
Good idea also, I'm thinking of using Vec. |
Beta Was this translation helpful? Give feedback.
Why are you storing your states in a
Map
-like structure? Consider using a fixed-size array if you know how many element there are going to be (or maximum number of elements) or aVec<S>
. Then you can mutably iterate through this. It will be much simpler, and it will be ordered correctly.or