• asudox@discuss.tchncs.de
    link
    fedilink
    arrow-up
    6
    arrow-down
    1
    ·
    il y a 25 jours

    That got me confused with rust references and why the dereference operator even exists as well.

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      3
      ·
      il y a 25 jours

      I mean, Rust has the additional thing that a reference is a pointer + a borrow, so it’s not quite as similar to a pass-by-value.

      And as for the dereference operator, occasionally you can use it to turn a reference into an owned value, often by making a copy of the value (but in that case, you can usually also use .to_owned()).

      A case where I don’t think there’s an alternative to the dereference operator, is if you’ve got a mutable reference and you want to replace the value that’s underneath: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=04621490e1d0fd6fe1af7f2e843547fb
      At the very least, if you remove the asterisk, the compiler will tell you very precisely that you need to add it back.

      • asudox@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        il y a 25 jours

        yeah. references aren’t the same as pointers in c++ but similar, so it’s something along those lines.