2

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

you are viewing a single comment's thread
view the rest of the comments
[-] lavafroth@programming.dev 0 points 1 year ago

If the data represented in the Arc is supposed to be immutable, why not pass around a reference of Vec<T> instead of cloning the Arc<[T]> over and over? The latter appears more expensive. Arc<String> is always going to be worse than Arc<str> because Arc<String> is roughly equivalent to Arc<Vec<char>>. Lastly, instead of the overhead of a tuple struct, we could define

pub type MonsterId = Arc<str>;

and maybe define and implement traits on it for additional methods.

[-] Dumhuvud@programming.dev 0 points 1 year ago

Arc<String> is roughly equivalent to Arc<Vec<char>>.

Nitpick: it's Vec<u8>, not Vec<char>.

[-] lavafroth@programming.dev 1 points 1 year ago

Gee thanks, nice catch!

this post was submitted on 18 Jun 2023
2 points (100.0% liked)

Rust

6049 readers
124 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS