this post was submitted on 08 Oct 2023
66 points (92.3% liked)

Rust

5931 readers
9 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 12 points 1 year ago (14 children)

Rust requires a mindset shift

That's easier said than done. I find that there's no clear vision of what "idiomatic" Rust is. With functional programming, it feels like there's a strong theoretical basis of how to structure and write code: pure functions everywhere, anything unpure (file access, network access, input conversion, parsing, etc.) goes into a monad. TBF, the only functional code I write is in JS or nix and nix-lang is... not really made for programming, nor is there any clear idea of what "good" nix code looks like.

Rust however... are Arc, Box, Rc, async, etc. fine? match or if/else? How should errors be handled? Are macros OK? Yes, the clippy linter exists, but it can't/won't answer those questions for you. Also the fact that there is no inheritance leads to some awkward solutions when there is stuff that is hierarchical or shares attributes (Person -> Employee -> Boss -> ... | Animal -> Mammal-Reptile-Insect --> Dog-Snake-Grasshopper). I haven't found good examples of solutions or guidance on these things.

My rust code still feel kludgy, yet safe after a year of using it.

[–] [email protected] 4 points 1 year ago* (last edited 1 year ago) (2 children)

@onlinepersona @snaggen

people write oo code in linux source code using c. With rust is simple. Just use composition, and you can explore the traits, that serves as and is better than interfaces.

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago) (1 children)

@onlinepersona @snaggen another problem I see people doing while writing rust, is trying to write code like java. Rust isn't an OO language, but you can organize your code and have hierarchies.

[–] [email protected] 6 points 1 year ago

Rust isn’t an OO language, but you can organize your code and have hierarchies.

IMO I think this is a common fallacy. OOP does not mean inheritance/hierarchies (despite them being part of every introductory OOP course nowadays). The original meaning of OOP had nothing to do with inheritance, that idea was mostly popularised by Java. And these days not even Java devs recommend inheritance as the first port of call but instead often favour composition and interfaces as the better language constructs.

Rust is as good at OOP styles as it is functional or procedural, if you ignore inheritance as a requirement of OOP. And a lot of code in rust can look and feel like OOP code in other languages. The abilities to encapsulate state, and polymorphism your code are far better features of OOP and both are well supported in rust. IMO rust offers the useful features from all paradigms fairly equally, which lets you write in any style you like, or even mix and match depending on the various situations. As one is not always better then the others, but each alone is useful in specific situations. More languages should be like this rather than forcing everything into one mold as it lets you pick the best style for each task.

load more comments (11 replies)