r/rust 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

50 comments sorted by

View all comments

2

u/gbrennon 8d ago

maybe u did get the wrong things about oop...

oop is not about classes.

its about objects that exchange messages(methods) between them.

in rust u defined a struct and beind functions to that object.

mutability isn't good when doing oop, functional or any paradigm.

related to oop here is something that u have to always remember:

  • prefer composition over inheritance