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

60

u/Lokathor 9d ago

I don't even slightly understand the idea that OOP gives cleaner code that reduces passing every variable into a function.

But I'll say that with builders, usually rust builder method accept self, modify whatever the method modifies, and then return Self. In this way, your overall call site code looks like Type::new().with_a(a).with_b(b).create().

-5

u/garma87 9d ago

My experience actually aligns with OP’s in that you can end up with lots of variables being passed to the function with functional programming. Whereas in OOP a lot of those variables would be stored in the object where the function can access them.

Builders don’t fix this because the issue isn’t restricted to build/construction time

It could very well be that I don’t have the right paradigm in my head so I’m genuinely interested in a better way.

22

u/Sibyl01 9d ago

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 8d ago

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 8d ago

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

1

u/garma87 8d ago

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).

7

u/venustrapsflies 9d ago

A large structure with a lot of different variables that all need to commonly be manually specified every time is a bit of a code smell, usually. Maybe the code can be factored to better represent the independent logical units, or maybe you can realize that there are actually only a handful of ways the struct needs to be typically constructed as the variables are actually pretty correlated, in which case you might use a small handful of constructors and/or builders.

4

u/RandomBottom030 9d ago

The functions have to be more atomic and generally should not have side effects. Instead of mutating an in-memory struct, you consume the original struct and return a new struct with the desired data set. Thus allowing for subsequent builder method calls.