Labfox's Lemmy
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
chromeleon@lemmy.world to Programmer Humor@programming.dev · 1 day ago

Me, after carefully reading Rust's ownership and borrow checker rules

lemmy.world

message-square
14
link
fedilink
233

Me, after carefully reading Rust's ownership and borrow checker rules

lemmy.world

chromeleon@lemmy.world to Programmer Humor@programming.dev · 1 day ago
message-square
14
link
fedilink
  • unalivejoy@lemmy.zip
    link
    fedilink
    arrow-up
    9
    ·
    22 hours ago

    I recently learned you can pass a &String to a &str parameter, so that’s neat.

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      6
      ·
      17 hours ago

      Ah yeah, via deref coercion, which is also called “auto-dereferencing” at times. Not to be confused with “auto-referencing”, which is also a thing[1].

      You can do some wild shit with deref coercion. And when I say “wild”, I guess, I’m talking about the most normal thing for Java devs, because well, it’s a lot like inheritance. 😅

      Basically, this concept of being able to pass &String into a parameter that takes &str also applies to the self parameter. Or in other words, methods implemented on str can also be called on String, as if String extends str.
      And well, obviously you can also make use of that yourself, by writing your own wrapper type. You can even “override” existing methods in a sense by re-defining them in the wrapper type.

      I had to play around a bit with it myself, so here’s a playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=af65ed396dec88c8406163acaa1f8f8d


      1. https://doc.rust-lang.org/nomicon/dot-operator.html ↩︎

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        4
        ·
        14 hours ago

        Welp, I posted my hot take that impl Deref is similar to inheritance as a meme in !rust@lemmy.ml: https://lemmy.ml/post/42514248

        Now, let’s see how many feathers get ruffled. 🙃

Programmer Humor@programming.dev

programmer_humor@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmer_humor@programming.dev

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1.05K users / day
  • 4.16K users / week
  • 8.33K users / month
  • 8.41K users / 6 months
  • 1 local subscriber
  • 29K subscribers
  • 312 Posts
  • 3.62K Comments
  • Modlog
  • mods:
  • adr1an@programming.dev
  • Feyter@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org