Skip to content
0:00 / 0:00

#rust#arc#rc

Use Arc Instead of Vec

Logan Smith

51K subscribers • 6 videos on Hallway

YouTube
  • Recommended by 7 people: 7 in engineering communities.
  • Presented by Logan Smith
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

239K views3 years ago7 recommendations

Programming languagesSystems programmingRustMemory managementPerformance and optimization

Logan Smith explains why and how to use Arc and Rc for immutable, reference-counted strings and dynamic arrays in Rust to avoid deep cloning.

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: serde.rs/feature-flags.html#-features-rc Arc docs: doc.rust-lang.org/std/sync/struct.Arc.html Vec docs: doc.rust-lang.org/std/vec/struct.Vec.html Smart pointers in Rust: youtube.com/watch?v=8O0Nt9qY_vo animations: manim.community/

Why it's here

7 people recommend this

What people say. Recommended for its clear explanation of using Arc and Rc for read-only cloning and string interning in Rust.

u/Dear_Spring7657in r/rust • Recommends

On “why is str a type if only &str is used?”

Logan Smith did a good video on the use of "str" in Arc<str> recently: video link

u/anlumoin r/rust • Recommends

On “str type weithout &”

Interesting video about this: link to this video

u/sharddbladein r/rust • Recommends

On “why is str a type if only &str is used?”

This video actually makes a case for why you should use Arc<str>/Rc<str> and Box<str> instead of &str for specific types of use-cases (spoiler: Rc<str> is much more performant for read-only cloning which makes sense when you think about it but I didn't think about it until watching the video).

u/kaczor647in r/rust • Recommends

On “Rust and data processing”

One of the opinions I've heard is that Rust has pretty good String handling, might be a good use case to parse your logs and output the JSON. If you're doing string parsing check out this video on Youtube [video It's called use Arc instead of Vec (the guy also gives an example on &str vs String), pretty neat.

u/Locaritoin r/rust • Recommends

On “Should I use a Box<str> instead of a String when possible?”

I think you should use `Rc<str>` instead (`Arc<str>` in multithreaded environments). You seldom need to mutate a string, if you do, you may need to grow it and reallocate anyway even if you use `String` because you exceed capacity. A good use case for `String` is when you use a sort of StringBuilder, for all the over you can use `Rc<str>` that allow to share memory immutably very efficiently. `Rc<str>` defers to `&str` so you can very easily use existing APIs. Logan Smith explains explains this very well

u/CoffeeVectorin r/rust • Recommends

On “Should I use a Box<str> instead of a String when possible?”

YouTube video for more details on this topic. link to this video And that this topic is called String Interning. https://en.wikipedia.org/wiki/String_interning

u/MalbaCatoin r/rust • Recommends

On “What are the most common mistakes and code-smells that newbies make?”

or, actually this is the perfect example for poor man's interning - `Arc<str>`. link to this video

u/Firakein r/rust • Mentions

On “Opinions on Lets Get Rusty?”

Hey man! Here’s a “real criticism” version of my original comment, with the caveat that I haven’t watched your stuff in a while so it could be different now: Let’s Get Rusty is generally pretty good at introducing people to Rust. It’s a lot of general overviews in bite sized pieces. As I advanced, I found I got annoyed that the content never got deeper. As a beginner, it’s nice to have narrated, strict explanations of concepts. But most of that information is available in the rust book, something that an intermediate rustacean should be familiar with reading. I appreciate content more that has interesting things to say about the language. I’m no longer just trying to understand the dif…

u/CrimsonManain r/rust • Mentions

On “Beginner code review: Convert comma-separated string to vector”

I agree. It's probably a bit overkill to use the `From` trait here. I just considered that it might be useful later, and it is a simple addition. So I watched an interesting video the other day about preferring `Arc<T>` over `Vec<T>`, which was an interesting watch. They use `Arc<str>` compared to `String`, but it's got me using `Rc<str>` when I can. And `Rc<str>` obscures the lifetime of the reference, which is a decent benefit. Here is the video if you're interested: Use Arc instead of Vec

u/Ciciboy1in r/rust • Mentions

On “Help with taking borrow checker and transforming a Vec<String> to Vec<&str>”

A Vec<&T> cannot simple leave a scope, since the elements are references to data inside the scope. Depending on what you want to do with this data you can either use a Vec<String> so the Vector owns the values. This is useful if you do not want to clone this vector (often) and/or need to modify its elements. Another thing you can use is a Vec<Rc<str>> (or Arc if you do multithreading). This makes cloning this vector much cheaper, as you are now just using references, basically the thing you wanted to achieve with &str. This does however make it so you cannot modify the strings. Here is a good video on the topic: link to this video

u/Intelligent_human_1in r/rust • Mentions

On “I've been writing Rust for 5 years and I still just .clone() everything until it compiles”

[video Check this out. It may help you