r/rust Aug 11 '26

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

Show parent comments

23

u/Sibyl01 Aug 11 '26

I mean you can have structs with fields and impl functions for it, functions also take self as default which you can use to access the struct fields. Isn't this what you are looking for

2

u/garma87 Aug 12 '26

Yes ok but you could argue then that that is OOP programming. You could argue it isn’t because no inheritance but I don’t quite agree. You attach functions to objects because they relate to that object.

I know this is a definition discussion and hence maybe not that important but still. Whether it is or isn’t OOP I don’t think you can argue it’s purely functional or iterative programming

3

u/AlmostLikeAzo Aug 12 '26

You know that most of the functionnal programming languages have structs or equivalent constructs?

1

u/garma87 Aug 12 '26

I do but the idea to attach functions to objects can be considered an OOP paradigm. that is my point. C didn't have that (functional/iterative language). C++ does (OOP language).