Question relating references

I'am just beginning with Rust, reading the book. I'm familiar with Java and C++ (Qt). Because I want a little bit more speed, I'm thinking about rewriting my program with Rust.

The program parses a script and produces images and other scripts (SVG+Python) to render more pictures.

There is a root object that holds all the data collected from the original script. Nearly all twigs and leafs get a reference to that root object to fetch information from it.

From what I have read so far, I ask me, if such a structure is possible to maintain in rust.

Example: The width of the pictures to produce is one of the informations hold by the root object. Twigs and leafs will read this information without changing it (immutable). But it is possible, that the script changes the format. If that happens, it would be early, but there will be already at least some twigs with a reference to the root object. But, as far as I understood, its not possible anymore to change the content of the root object (struct) when there are any immutable references. So I'm more than a little bit confused, if it is possible to structure my program as before.

Is there any way in Rust to build such a root object?

Closest equivalent of Java's objects in Rust is Arc<Mutex<T>> or Rc<RefCell<T>> (both are the same thing, but multi-threaded or single-threaded). If you wrap every one of your objects in it, you'll be able to use more-or-less Java-style programming patterns, but that will of course litter your types with this boilerplate.

Having a root object will be a pain in Rust, because mutable access requires exclusive access, and exclusive access to the root object will mean only one thing in your program can work at a time.

So Rust's approach is to avoid root objects as much as possible. Split program's state and configuration into many small independent structures.

1 Like

Don't use object orientated design. ie the structures "root, twigs and leafs" are independent from the algorithm "read this information"

Thank you for the replies. Seems so, that I have to rethink either my design or to switch to Rust.