I’m porting a Java program to Rust. It simulates balls bouncing around, some of them bonded in pairs. In the Java version, the simulated world has a list of balls and also a list of bonds, where each bond references the balls it connects. The world tells the bonds to add spring forces to the balls, then tells the balls to update their positions based on the forces on them. (Other forces come from the balls hitting walls and each other.)
I can’t port this structure as-is to Rust because it can have multiple, long-lived, mutable references to each ball. The best alternative I’ve thought of so far is for the world to keep a table of balls indexed by some sort of id. Bonds would then refer to balls by id instead of by reference. Seems like this should work, though I haven’t tried it, but basically all I’ve done is create a sort of poor man’s reference so I can create a “sea of objects”, which the O’Reilly book recommends against. Is there a more idiomatic way to create this sort of object graph?