Iterating over a field of mutable struct

Hi
I’m trying to port some code to Rust to learn the language and hitting a wall here. Would appreciate any advice.

Here’s what I’m trying to do.
I have a struct State that represents a global mutable state (does not need to be thread-safe) of the system and also contains a vector of components each having it’s own state.

I’m trying to write a funciton that would update the global state and the state of each component.
The minimal version looks like this

struct Component;

impl Component {
   fn update(&mut self, _state: &mut State) { }
}

struct State {
   components: Vec<Component>
}

impl State {
   fn update(&mut self) {
       // update self
       for component in self.components.iter_mut() {
           component.update(self);
       }
   }
}

(Playground)

Is there any way to make it work?
Appreciate this is probalby not the most idiomatic way, my plan was to first port the orignal code (from java) as close as possible and then refactor it.

Thanks.

Consider defining a separate type that holds the state you'd like Component to be able to mutate, and then pass that in. E.g.:

struct Component;

impl Component {
    fn update(&mut self, _state: &mut UpdateState) {}
}

// Put whatever inside this
struct UpdateState;

struct State {
    components: Vec<Component>,
    update_state: UpdateState,
}

impl State {
    fn update(&mut self) {
        // update self
        for component in self.components.iter_mut() {
            component.update(&mut self.update_state);
        }
    }
}

I can see the initial appeal of this approach, but you'll likely fall into a bunch of traps along the way if the minimal example you gave is representative of how the Java code does things. It might be easier (and less frustrating) to port the functionality, but not the structure - instead, try to write/structure things the Rust way from the get-go.