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

  • nous@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    It is cheaper to copy a Id(u32) then a Id(Arc<str>) The Arc requires a lock, incrementing a counter and copying a pointer. The Id is only a copy of a u32. For the use case the video describes it makes more sense to use simple IDs and not stringy typed ids for passing around everywhere to avoid this extra cost (as well as the cost of comparing strings when you need to fetch the data the id is meant to represent).