• r00ty@kbin.life
    link
    fedilink
    arrow-up
    10
    ·
    1 hour 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.

  • vext01@lemmy.sdf.org
    link
    fedilink
    arrow-up
    3
    ·
    2 hours ago

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

  • flying_gel@lemmy.world
    link
    fedilink
    arrow-up
    12
    arrow-down
    1
    ·
    4 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;

  • Ashelyn@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    21
    ·
    5 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)?

    • Rusty 🦀 Femboy 🏳️‍🌈@lemmy.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      11
      ·
      3 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
      link
      fedilink
      arrow-up
      28
      ·
      5 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.

    • Dhs92@programming.dev
      link
      fedilink
      arrow-up
      8
      ·
      4 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
        link
        fedilink
        arrow-up
        6
        ·
        4 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

        • Wappen@lemmy.world
          link
          fedilink
          arrow-up
          8
          ·
          3 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.

  • Gonzako@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    4 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

      • GetOffMyLan@programming.dev
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        2 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
        link
        fedilink
        arrow-up
        1
        arrow-down
        2
        ·
        edit-2
        2 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
          link
          fedilink
          arrow-up
          2
          ·
          1 hour 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
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 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);