this post was submitted on 15 Nov 2024
286 points (93.1% liked)

Programmer Humor

32571 readers
131 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
[โ€“] Ashelyn@lemmy.blahaj.zone 33 points 1 week ago (3 children)

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

[โ€“] mvirts@lemmy.world 47 points 1 week ago (1 children)

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 19 points 1 week ago

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

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.

[โ€“] Dhs92@programming.dev 23 points 1 week ago (1 children)

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 10 points 1 week ago (2 children)

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

[โ€“] Wappen@lemmy.world 19 points 1 week ago (1 children)

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 week ago (1 children)

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.

[โ€“] Dhs92@programming.dev 2 points 1 week ago

Not really related to the pointer thing, but Rust also has pattern matching based on Enums, as they're actually sum-types and not just numbers

[โ€“] 2xsaiko@discuss.tchncs.de 5 points 1 week 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.