Skip to content

How do I store states in dynamic collections? #926

Answered by yusdacra
madmaxio asked this question in Q&A
Discussion options

You must be logged in to vote

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 a Vec<S>. Then you can mutably iterate through this. It will be much simpler, and it will be ordered correctly.

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));
}

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by hecrj
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #921 on June 22, 2021 09:53.