Rust lets you do efficient reference-counted strings and dynamic arrays using Arc basically just as easily as their owning (and deep-cloning) equivalents, String and Vec respectively. So why not use them as a reasonable default, until you actually need the mutability that String and Vec provide? Get into the weeds with me here, feat. some cool visualizations, with special guest appearance from Box.

This video assumes some familiarity with Rust and its core smart pointer types, namely Vec/String/Rc/Arc/Box, along with data structures like HashMap and BTreeMap, and traits like Clone, Hash, Ord, and serde::{Serialize, Deserialize}.

serde feature flag for Rc/Arc
Arc docs
Vec docs
Smart pointers in Rust • Crust of Rust
animations

  • Vorpal@lemmyrs.org
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    1 year ago

    I believe the video is somewhat incorrect: Arc (and Rc) need a reference count, which you missed in your description of the overhead. (EDIT: You mentioned this later in the video. )

    The downside of all of Arc or Rc is of course (as you pointed out towards the end of the video) that you need to do all that reference counting and store the reference count (as I mentioned above). A box of a slice would indeed be cheaper. But if the data is static (or will at least live for the rest of the program), you might consider just leaking it (Box::leak) to get a static lifetime reference. That can then be cheaply shared.

    • ericjmorey@programming.devOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      I want to be clear that I didn’t make this video. I just thought it was worthy of sharing as someone who doesn’t have a good handle on Rust or best practices in using Rust.