The Secret to Rust Ownership: Rc vs. Arc

Ғылым және технология

Rc and Arc are two magic and powerful methods of taming Ownership in Rust. Next to this complexity, Rust also has really easy to use one line tricks. If you are interested, I recommend watching this video after understanding the move keyword: • Master Rust File Handl...
📝 Description
Discover the ins and outs of Rust's powerful reference counting mechanism with Rc and Arc in this comprehensive video. Join me as we delve into the world of memory management in Rust and explore how Rc and Arc enable efficient memory sharing and prevent memory leaks. We'll cover the differences between Rc and Arc, their use cases, and best practices for leveraging them in your Rust projects.
Important Note: using `.unwrap` without proper error handling is not recommended and should be avoided. I only use `.unwrap` for demonstration purposes in this video.
⏳ Timestamps:
00:00 - Introduction
01:44 - Non-working example
04:28 - Rc Example
10:07 - Arc Example
13:23 - Outro
🔗 Important links:
Rust Rc: doc.rust-lang.org/std/rc/stru...
Rust Arc: doc.rust-lang.org/std/sync/st...
👋 Hey there!
If you are new to this channel: Hey 👋 my name is Flo and I am a professional software engineer with a passion for coding in Golang, TypeScript, JavaScript, and Rust. This channel is where I share that passion and dive into the exciting world of software engineering. Plus, I love using Emojis and paper-based animations (so have a look for that, if you want to find my videos).
📨 Support and Connect!
Buy me a coffee: www.buymeacoffee.com/florianw...
Become a member: www.youtube.com/@flowoelki/me...
Discord: / discord
Instagram: / flowydev
LinkedIn: / florian-woelki
TikTok: / florianwoelki
GitHub: github.com/FlorianWoelki
#Rust #Arc #Rc #ReferenceCounting #MemoryManagement

Пікірлер: 12

  • @codingcrashkurse6429
    @codingcrashkurse64294 ай бұрын

    He is back! Nice to see you also switched to english. And that you work with rust ;-)

  • @kingkolya9842
    @kingkolya98423 ай бұрын

    Thank you for the clear explanations. What would be a use case for using Refcell? Using a Mutex for a multi-threaded application can be understoof easily, but I have a hard time to grasp when to use refcell instead of having a mutable reference.

  • @FloWoelki

    @FloWoelki

    3 ай бұрын

    thank you for the question. i think a good use case for `RefCell` would be when you want to modify data with a shared struct. for instance, if we want to clone a struct but we want to keep track of the reference and whenever we manipulate something from the cloned struct, it will update the original one. maybe this code helps to better understand the use case: ``` struct Counter { value: RefCell, } fn main() { let counter = Rc::new(Counter { value: RefCell::new(0) }); let counter_clone = counter.clone(); // We can modify the counter even though it's shared: *counter_clone.value.borrow_mut() += 1; println!("Counter value: {}", counter.value.borrow()); } ```

  • @jay.rhoden
    @jay.rhoden4 ай бұрын

    What plugin makes the errors appear right at the end of the line like that?

  • @xyangst

    @xyangst

    4 ай бұрын

    Error lens

  • @xyangst

    @xyangst

    4 ай бұрын

    Nvm its nvim umm works out of the box i think

  • @FloWoelki

    @FloWoelki

    4 ай бұрын

    exactly. you could try "Error lens" for vs code. for nvim, i personally use zero-lsp which also shows me the error inline.

  • @jay.rhoden

    @jay.rhoden

    4 ай бұрын

    @@xyangst Hmmm, I use neovim and it doesn't do that for me with lsp turned on.

  • @FloWoelki

    @FloWoelki

    4 ай бұрын

    you also need to enable the inline diagnostics (ref to this link: neovim.io/doc/user/diagnostic.html#vim.diagnostic.config()). hope that helps :)

  • @KhodeCamp
    @KhodeCamp3 ай бұрын

    Thank you for your video! but I don't completely understand by you println before v.push() then you expect to print new result included value from v.push(). how about this code? fn c

  • @FloWoelki

    @FloWoelki

    3 ай бұрын

    awesome, that you've liked this video :) i am not 100% sure what you mean with your question, but i'll try to answer it through reading the code and result :D the reason, why things are not mutate correctly is because we are cloning the vector with all its elements into a new memory address. so we basically do a copy paste and only mutate the vector inside of the closure.

Келесі