Whenever I encounter an interesting Rust programming technique, I add it to this blog post. I’ve amassed a bit of a collection. Hopefully someone finds it interesting and useful!

  • hatchetOP
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    Well, actually I would tend to agree that &[T] is preferable to AsRef in most cases; all of the smart pointers you mentioned can also easily be turned into plain references. I probably could have chosen a better example.

    • Justin Ossevoort@toot.community
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      @hatchet That is true and I mostly agree with you. AsRef makes function signatures more complex and you effectively change a simple function into a generic function.

      That said, when using some crates that make heavy use of this construction (especially when working with owned values in call chains) is sometimes significantly more ergonomic.

      So AsRef definitely had it’s uses, but I agree that it probably shouldn’t be the recommended “best practice” for all crates.