this post was submitted on 08 Oct 2023
63 points (92.0% liked)

Rust

6063 readers
18 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] 11 points 1 year ago (4 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] 24 points 1 year ago (2 children)

Rust however… are Arc, Box, Rc, async, etc. fine?

Yes, these are all fine to use. When you should use them depends on what you are doing. Each of these has a usecase and tradeoffs associated with them. Rc is for when you need multiple owners to some heap allocated data in a single threaded only context. Arc is the same, but for multithreaded contexts (it is a bit more expensive than an Rc). Box is for single owner of heap allocated data, async is good for concurrent tasks that largely wait on IO of some sort (like webservers)

match or if/else?

Which ever is more readable for your situation. I believe both are equally powerful, but depeding on what you are doing you might find a simple if let enough, other times a match makes things a lot more succinct and readable. There are also a lot of helper functions on things you often match on - like Result and Option that for specific situations can make a lot more readable code. Just use which ever you find most readable in each situation.

How should errors be handled?

This is a large topic. There is some basic advice in chapter 9 of the book though that is mostly about Result vs panic. There are also quite a few guides out there about error handling in rust in a broader sense.

Typically they suggest using the thiserror crate for errors that happen further from main (where you are more likely to care about individual error variants - ie treating a file not found differently from a permission denied error) and the anyhow crate or eyre crate for errors closer to main (when you are dealing with lots of different error types and don't care as much about the differences as you typically want to deal with them all in the same way when you are near to main).

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 have rarly seen a system that maps well to an inheritance based structure. Even the ones you give are full of nuances and cross overs that quickly break apart. The classic animal example for instance falls flat on its face so quickly. Like, how do you organise a bird, a reptile, a dog, a fish and a whale? You can put the whale and dog under mammal, but a whale shares a lot of things with the fish, like its ability to swim. Then think about a penguin? It is a bird that cannot fly, so a common fly method on a bird type does not make any sense as not all birds can fly. But a penguin can swim, as can other birds. Then just look at the platypus... a mammal with poison spurs that swims and lays eggs, where do you put that? Where do you draw the lines? Composition is far easier. You can have a Fly, Swim, Walk etc trait that describe behaviour and each animal can combine these traits as and when they need to. You can get everything that can fly in the type signature, even if it is a bird, a bat, or even an insect. Inheritance just cannot do that with the number of dimensions at play in any real world system.

IMO it is simpler and makes more sense to think in terms of what something can do instead of what something inherits from.

[–] sugar_in_your_tea 5 points 1 year ago

Precisely. I work in Python a lot, and almost all of our classes use mixins because there's single inheritance rarely is that answer. And that's basically just an abuse of inheritance and a trait system would probably be a better fit.

[–] [email protected] 5 points 1 year ago (1 children)

I recently caught myself trying to do traits in an OOP language. Failed spectacularly of course.

But it would be so much easier to read…

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

Depending on your language, your closest analogue is going to be interfaces. C# even has a where clause where you can restrict a generic type such that any type substituted must implements one or more interfaces. You can get quite a bit of trait like working there, from the function input side of stuff.

The biggest problem is, you can't implement an interface for a type unless you have access to the type. So you'd have to really on wrapping types you don't own to apply trait like interfaces to it.

And then there's minor issues like, no such thing as associated types, or being able to specify constants in a definition. But you can usually work around that in a less nice way.

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

In my case, it was in Dart. Dart allows extending existing classes with new methods, but unfortunately this doesn't allow implementing abstract mixins (which is the equivalent of Rust's trait) on other types. Dart is in this weird middle where it's not really strictly typed (it has dynamic, which is like the any type in TypeScript), but the compiler doesn't allow ducktyping anyways.

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

Sometimes I wonder if this pure search for being "idiomatic" is worth the effort. On paper yes, more idiomatic code is almost always a good thing, it feels more natural to create code in a way the language was designed to be used. But it practice, you don't get any points for being more idiomatic and your code isn't necessarily going to be safer either (smart pointers are often "good enough"). I'm fine using references to pass parameters to function and I love the idea to "force" the programmer to organize objects in a tree way (funny enough I was already doing that in C++), but I'll take a Rc rather than a lifetimed reference as a field in a structure any day. That shit becomes unreadable fast!

EDIT: but I love cargo clippy! It tells me what to change to get more idiomatic points. Who knows why an if/then/else is better than a match for two values, but clippy says so, and who am I to question the idiomatic gods?

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

@Blackthorn @onlinepersona Feels the best when you don't have to do either.

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago) (1 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] 0 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] 5 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.

[–] [email protected] 0 points 1 year ago (2 children)

If you want inheritance you can add structs as parameters of other structs. This will also allow them to use impl functions for that struct.

As far as I understand it Arc<> is just the Async version of RC<>.

I'm not entirely sure about Box<> and a lot of its API's are still unstable but I believe it's primarily used as an owner for unsafe things.

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

As far as I understand it Arc<> is just the Async version of RC<>.

Not quite right. Arc is the atomic RC, aka the one that is thread safe and can thus can be sent to other threads. Rc is single threaded only. async is agnostic of threading and there exists runtimes that are both multithreaded and single threaded.

Although tokio, the most common async runtime, is multi threaded and so tasks you create need to be multi-threaded safe thus you likely need to use Arc for most things in tokio. But that is due to the multithreaded nature of tokio, not the fact it is async.

I’m not entirely sure about Box<> and a lot of its API’s are still unstable but I believe it’s primarily used as an owner for unsafe things.

Box is the single ownership heap allocated datastucture of rust. It is a core type that is used for a lot of things. It is not just for unsafe things and mostly safe things are put in a Boxes. It is basically used whenever you want something on the heap rather than the stack At least when the type in question is not already heap based (like Strings, Vec, Arcs etc).

It is used a lot for trait objects (when the type is only known at runtime, not compile time, aka Box&lt;dyn Trait>/&amp;dyn Trait) when you need ownership of the object (aka reference trait objects are not suitable). Or when you have a large type that needs to be moved around a lot and you don't want expensive stack copies when a cheap copy of a pointer to some heap data will do instead. Or you have a type that is not Sized (aka the size is not known at compile time and needs to be tracked at runtime) but need it to be owned (such as a slice, trait objects etc).

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

Ahh, thank you for the corrections.

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

Box is (basically) just the way to have memory on the heap. Here's a direct comparison of how to do heap memory in C/++ and in rust:

int* intOnHeap = (int*)malloc(sizeof(int));
*intOnHeap = 0;
MyClass* classOnHeap = new MyClass();
let intOnHeap: Box = Box::new(0);
let structOnHeap: Box = Box::new(MyStruct::default());

There can be a bit more to it with custom allocators etc. but that's the only way most people will use boxes. So Box basically just means "a T is allocated somewhere and we need to free that memory when this value is dropped".