this post was submitted on 15 Nov 2024
281 points (93.5% liked)

Programmer Humor

32483 readers
377 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] r00ty@kbin.life 69 points 2 days ago (7 children)

The problem with rust, I always find is that when you're from the previous coding generation like myself. Where I grew up on 8 bit machines with basic and assembly language that you could actually use moving into OO languages.. I find that with rust, I'm always trying to shove a round block in a square hole.

When I look at other projects done originally in rust, I think they're using a different design paradigm.

Not to say, what I make doesn't work and isn't still fast and mostly efficient (mostly...). But one example is, because I'm used to working with references and shoving them in different storage. Everything ends up surrounded by Rc<xxx> or Rc<RefCell<xxx>> and accessed with blah.as_ptr().borrow().x etc.

Nothing wrong with that, but the code (to me at least) feels messy in comparison to say C# which is where I do most of my day job work these days. But since I see often that things are done very different in rust projects I see online, I feel like to really get on with the language I need a design paradigm shift somewhere.

I do still persist with rust because I think it's way more portable than other languages. By that I mean it will make executable files for linux and windows with the same code that really only needs the standard libraries installed on the machine. So when I think of writing a project I want to work on multi platforms, I'm generally looking at rust first these days.

I just realised this is programmerhumor. Sorry, not a very funny comment. Unless you're a rust developer and laughing at my plight of trying to make rust work for me.

[–] Ephera@lemmy.ml 7 points 1 day ago (1 children)

I find these videos give a very visual explanation and help to put you into the right mindset: http://intorust.com/
(You can skip the first two videos.)

Sort of when it clicked for me, was when I realized that your code needs to be a tree of function calls.
I mean, that's what all code is anyways, with a main-function at the top calling other functions which call other functions. But OOP adds a layer to that, i.e. objects, and encourages to do all function calls between objects. You don't want to do that in Rust. You kind of have to write simpler code for it to fall into place.

To make it a bit more concrete:
You will have functions which hold ownership over some data, typically because they instantiated a struct. These sit at the root of a sub-tree, where you pass access to this data down into further functions by borrowing it to them.

You don't typically want to pass ownership all over the place, nor do you typically want to borrow (or pass references) to functions which are not part of this sub-tree.
Of course, there's situations where this isn't easily possible, e.g. when having two independent threads talking to each other, and then you do need Rc or Arc, but yeah, the vast majority of programming problems can be solved with trees of function calls.

[–] r00ty@kbin.life 2 points 1 day ago

Sort of when it clicked for me, was when I realized that your code needs to be a tree of function calls.
I mean, that’s what all code is anyways, with a main-function at the top calling other functions which call other functions. But OOP adds a layer to that, i.e. objects, and encourages to do all function calls between objects. You don’t want to do that in Rust. You kind of have to write simpler code for it to fall into place.

Yes, this ties in with what I'm saying though. You need a paradigm shift in your design philosophy, which is hard when you come from a Cx background.

I also think that in OO there shouldn't be much cross contamination. It happens (and it happens a lot in my personal projects to be fair) but when well designed it shouldn't need to be. In C# for example it should be the case that rather than a function owning a resource, a class should. So when using an object between classes you take it as a reference from a method in one class and pass it into a method to another class rather than call that class and make it a dependency of that class too. In this way you would have a one way dependency, rather than a two way.

This kind of thinking has moved into creating objects in rust. Also I think yes within a same class the idea of a function (that isn't static) accepting an object that is part of the class that was returned by another function in the case class feels very wrong from a Cx style point of view. If we knew we were going to do that, we'd just make it a class level variable and use it in both functions.

Like I say, just another way of thinking and I'm not there yet.

load more comments (5 replies)