Can you store references to all the elements of a list?

⚓ Rust    📅 2026-04-01    👤 surdeus    👁️ 6      

surdeus

Sorry for the vague title as I can't think of any way to phrase it any better but here is my situation.

I am currently writing a 3D rigid body physics simulator. For this here is my brief architecture:
There are broadly three structs for better organization: World struct which hold the world properties like gravity, etc etc but more importantly a list of bodies. Body struct which defines a single body it hold the body properties like mass, current state. And for simpler organization I also have State struct. Since the state of the body is defined by its 3D position, Orientation, linear velocity and angular velocity. This is the reason I decided to create a struct called State and each body thus have an instance of a State struct.
Due to this architecture the world instance will have a list of body struct. As shown below:

pub struct State {
    pub position: DVec3,
    pub velocity: DVec3,
    pub orientation: DQuat,
    pub angular_velocity: DVec3,
}

pub struct Body {
    pub id: u32,
    .....
    pub state: State,
}

pub struct World {
    .....
    pub bodies: Vec<Body>,
    .....
}

What I am asking is that is it possible to create a property within world which will store the references to the states of all the bodies within the world struct? Or am I better of looping through the list of bodies when I need it?

The reason I am asking this is because for a new physics solver I am writing I need all the states in a single linear algebra equation. This way I can use linear algebra to solve it. I am still figuring out the implementation on paper first for this so no code yet.

Just my question is should I have another property within the World struct which holds the reference to all the states of the bodies the world instance currently holds. Is this a viable architecture? Or am I overcomplicating things? This is my first rust project of this scale so I don't have much experience. I believe I do have fundamental understanding of Ownership, Burrowing and lifetimes (I hope).

7 posts - 4 participants

Read full topic

🏷️ Rust_feed