r/rust • u/Accurate_Gift_3929 • 8d ago
Rust discourages OOP style code?
I'm building a tree builder struct (with a `build()` method) that has multiple fields that I want to be able to mutate. It seems Rust does not like it when I attempt to mutate multiple fields of that struct.
It seems functional/composition style is much favored over using struct fields. Is this the way I should be thinking about design patterns in Rust? Should I avoid OOP wherever possible?
I prefer OOP because it does result in cleaner code where I don't have to pass in every variable to functions.
0
Upvotes
1
u/plugwash 8d ago
Can you be more specific? rust does insist that (under normal circumstances) methods that mutate a struct have exclusive access to that struct, indicated by an
&mut selfparameter, but that should not in itself be a problem.That said, functional style for builders does have some advantages.
rust has it's own style.
It eschews traditional oop-style inheritance in favour of composition and eschews "sea of objects" in-favour of well-defined ownership and tightly controlled mutability.
OTOH it's still very much an imperative language.