138
I love Rust (lemmy.blahaj.zone)
all 39 comments
sorted by: hot top controversial new old
[-] fmstrat@lemmy.nowsci.com 8 points 1 hour ago* (last edited 1 hour ago)

Fun story from before Rust was getting popular (years ago). So, I did a performance comparison to determine what language we should write our rules engine in. I compared Go, Rust, Node, and some others not worth mentioning.

At the time, I had experience with all but Rust.

Even knowing nothing, and working from scratch, the Rust POC was significantly faster. Just way, way, better.

That being said, I still chose Go due to productivity based on the language knowledge of the team to ease the transition (Go was closer to what they knew already), and while it was good for them to learn Go, I look back on it and realize Rust would have been a great opportunity to invest in their careers and have them learn it instead.

A hindsight is 20/20 experience for me.

[-] akkajdh999@programming.dev 8 points 2 hours ago

In Go "==" operator works for everything by default, I like it more:

type A struct {
	Name string
	Quality int
}
func main() {
	var x A
	var y A
	fmt.Printf("%v", x == y)
}

(if all you want is to compare all corresponding fields which you usually want)

[-] PeriodicallyPedantic@lemmy.ca 1 points 1 hour ago

I mean, if your IDE does it for you, is it really that much better that it's shorter?

[-] ReversalHatchery@beehaw.org 0 points 40 minutes ago

not the IDE, its the compiler. this is also not some AI shit, in many cases (not all) the compiler can actually figure out how to do this, because it's not hard, it would just be a lot of boilerplate if written manually.

[-] PeriodicallyPedantic@lemmy.ca 1 points 14 minutes ago

Why did you even bring up AI? IDEs have been able to generate equality functions for decades without AI.

It's kinda neat to have this defined directly in the language so that compilers can implement it, but creating equality function is so low effort that this doesn't really seem like a big deal.

Like, you define the members in a class, then you tell your IDE to generate getters, constructor, equals, hashcode, etc all in like 5 seconds.
I like it, it's nice when the language itself defines reasonable defaults for things, but realistically you're saving yourself a few seconds of effort.

[-] r00ty@kbin.life 30 points 5 hours ago

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.

[-] beeng@discuss.tchncs.de 4 points 3 hours ago* (last edited 3 hours ago)

Go is really good for std library, windows and Linux from same code and static binaries BTW.

[-] 2xsaiko@discuss.tchncs.de 5 points 3 hours ago

Do you have some public code you could link to that you’re having this issue with? There isn’t a one-size-fits-all solution for Rc/RefCell, I think.

[-] r00ty@kbin.life 7 points 3 hours ago* (last edited 3 hours ago)

The current thing I'm working on (processor for iptv m3u files) isn't public yet, it's still in the very early stages. Some of the "learning to fly" rust projects I've done so far are here though:

https://git.nerfed.net/r00ty/bingo_rust (it's a multi-threaded bingo game simulator, that I made because of the stand-up maths video on the subject).
https://git.nerfed.net/r00ty/spectrum_screen (this is a port of part of a general CPU emulation project I did in C#, it emulates the ZX spectrum screen, you can load in the 6912 byte screens and it will show it in a 2x scaled window).

I think both of these are rather using Arc<RwLock<Thing>> because they both operate in a threaded environment. Bingo is wholly multi-threaded and the spectrum screen is meant to be used by a CPU emulator running in another thread. So not quite the same thing. But you can probably see a lot of jamming the wrong shape in the wrong hole in both of those.

The current project isn't multi-threaded. So it has a lot of the Rc/Rc<RefCell> action instead.

EDIT: Just to give the reason for Rc<RefCell> in the current project. I'm reading in a M3U file and I'm going to be referencing it against an Excel file. So in the structure for the m3u file, I have two BtreeMaps, one for order by channel number and one by name. Each containing references to the same Channel object.

Likewise the same channel objects are stored in the structure for the Excel file that is read in (searched for in the m3u file structure).

BTreeMaps used because in different scenarios the contents will be output in either name order or channel order. So just better to put them in, in that order in the first place.

[-] vext01@lemmy.sdf.org 11 points 5 hours ago

But then you realise that the types of 10 constituent fields don't implement Eq, PartialEq...

[-] flying_gel@lemmy.world 26 points 7 hours ago

I do appreciate how newer C++ standards have made these kinds of things a lot easier too.

Define all comparison operators with just one one line using C++20

auto operator<=>(const ClassName&) const = default;

[-] xor@lemmy.blahaj.zone 22 points 6 hours ago

It's nice that this exists these days, but my god is it horrendously unreadable at a glance

[-] GetOffMyLan@programming.dev 15 points 5 hours ago

That is completely incomprehensible lol

[-] tetris11@lemmy.ml 3 points 3 hours ago

You just need to break the syntax apart and look at it from the LHS and the RHS seperately.

In layman's terms: constantine felt boxed in by his social class which left him often at dagger-ends to the operations on his car. Unable to keep up with the constant payments, he defaulted on the loan.

See? Easy.

[-] 2xsaiko@discuss.tchncs.de 1 points 3 hours ago

Whoa nice, I need to keep this in mind.

[-] riodoro1@lemmy.world 3 points 6 hours ago
[-] MyNameIsRichard@lemmy.ml -5 points 6 hours ago

Only if you're a bad programmer :/

[-] GetOffMyLan@programming.dev 16 points 5 hours ago

This argument just doesn't hold up. Software written by some of the best developers in the world still has these same bugs.

Why even use a language where you have to put so much effort into something that comes for free in many modern languages.

[-] Kacarott@aussie.zone 18 points 7 hours ago
[-] egerlach@lemmy.ca 2 points 1 hour ago

Ahh, the comment I was looking for

I would have also accepted: "Haskell did it first."

[-] toastal@lemmy.ml 3 points 2 hours ago

OCaml has ppx_deriving. PureScript has derive instance.

[-] devfuuu@lemmy.world 2 points 3 hours ago

Newer Scala follows rhe same when enabling strict equaility. It's a goos thing.

[-] Ashelyn@lemmy.blahaj.zone 27 points 8 hours ago

Is that because it's that simple, or just that the boilerplate is pre-written in the standard library (or whatever it's called in rust)?

[-] rustyfemboy@lemmy.blahaj.zone 18 points 7 hours ago

Yes, it is that simple. In Rust if you have a structure Person and you want to allow testing equality between instances, you just add that bit of code before the struct definition as follows:

#[derive(PartialEq, Eq)]
struct Person {
    name: String,
    age: u32,
}

In Rust, PartialEq and Eq are traits, which are similar to interfaces in Java. Manually implementing the PartialEq trait in this example would be writing code that returns something like a.name == b.name && a.age == b.age. This is pretty simple but with large data structures it can be a lot of boilerplate.

There also exist other traits such as Clone to allow creating a copy of an instance, Debug for getting a string representation of an object, and PartialOrd and Ord for providing an ordering. Each of these traits can be automatically implemented for a struct by adding #[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord)] before it.

[-] mvirts@lemmy.world 35 points 8 hours ago

It's because people put in the hard work of writing amazing macros instead of baking code reuse into the type system itself 😁 I'm a rust noob and I love the derive macro.

[-] Ashelyn@lemmy.blahaj.zone 12 points 7 hours ago

So it's actually a secret third option! That's pretty rad.

[-] Dhs92@programming.dev 14 points 7 hours ago

Derive macros are a godsend. There's macros to automatically implement serialization as well. Basically a Trait that can automatically be implemented when derived

[-] Dunstabzugshaubitze@feddit.org 7 points 7 hours ago

i've only read about rust, but is there a way to influence those automatic implementations?

equality for example could be that somethings literally point to the same thing in memory, or it could be that two structs have only values that are equal to each other

[-] 2xsaiko@discuss.tchncs.de 2 points 3 hours ago

Not for the built-in Eq derive macro. But you can write your own derive macros that do allow you to take options, yeah.

[-] Wappen@lemmy.world 11 points 6 hours ago

Equality in rust is value equality per default, that's what these traits are for. If you want to check pointer equality you'd use the std::ptr::eq function to check if two pointers are equal, which is rather rare in practice. You can also implement the PartialEq trait yourself if you need custom equality checks.

[-] brisk@aussie.zone 1 points 1 hour ago

I worked on software at one point that had at it's core a number of "modes" that it switched between. It was, at the time, in the process of migrating from enums and switch/case trees to an inheritance based system.

In practice this meant there was a single instance of "Mode" for each mode which used pointer equality to switch/case on modes like an enum.

To add a new mode (that did nothing) I think I had to change about 6 different places.

[-] Gonzako@lemmy.world 3 points 7 hours ago

I've only had to implement equality in C# but that didn't seem that hard of a problem. you just expand the operator = function

[-] porous_grey_matter@lemmy.ml 8 points 6 hours ago

It's not hard, just if you're doing it for a struct with a lot of fields it's a lot of boilerplate

[-] GetOffMyLan@programming.dev 3 points 5 hours ago* (last edited 5 hours ago)

I just use the HashCode class and compare the results.

Pretty sure there's a source generator for it as well nowadays.

[-] Deckweiss@lemmy.world -3 points 5 hours ago* (last edited 5 hours ago)

My IDE can do that for me. And it was able to do that pre AI boom. Yes, the code ends up more verbose, but I just collapse it.

So from a modern dev UX perspective, this shouldn't be a major difference.

[-] porous_grey_matter@lemmy.ml 3 points 5 hours ago

Even if the tool works perfectly, you have to run it every time you change something. It's not the end of the world, but it's still much nicer to just have a macro to derive it at compile time.

[-] copygirl@lemmy.blahaj.zone 4 points 5 hours ago

Then you should also override Equals(object), GetHashCode, and implement IEquatable<T>.

Thankfully a lot of the usual boilerplate code can be avoided using a record class or struct:

public record Person(string Name, uint Age);
[-] Gonzako@lemmy.world 1 points 1 hour ago

Oh well, It does show how little I do have to actually use that. It just hasn't come up that much

this post was submitted on 15 Nov 2024
138 points (92.6% liked)

Programmer Humor

32472 readers
589 users here now

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

Rules:

founded 5 years ago
MODERATORS