The Secret to Rust Ownership: Rc vs. Arc

Поділитися
Вставка
  • Опубліковано 6 лис 2024

КОМЕНТАРІ • 25

  • @lMINERl
    @lMINERl 4 місяці тому +3

    This vid is gold 🥇 it covers everything
    Info ✔️
    Visual representation ✔️
    No jokes ✔️
    Step by step through errors ✔️
    Covers pitfalls you might encounter ✔️
    Different implementation ✔️

    • @FloWoelki
      @FloWoelki  4 місяці тому

      Thank you so much, really appreciate it :)

  • @jeremietamburini
    @jeremietamburini 19 днів тому

    Great explanation, thank you so much! After only 3 minutes of this video I clicked the subscribe button 😃👍

    • @FloWoelki
      @FloWoelki  17 днів тому

      That's so awesome to hear! Glad you've liked it :)

  • @codingcrashkurse6429
    @codingcrashkurse6429 9 місяців тому

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

  • @jay.rhoden
    @jay.rhoden 9 місяців тому +2

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

    • @xyangst
      @xyangst 9 місяців тому +1

      Error lens

    • @xyangst
      @xyangst 9 місяців тому

      Nvm its nvim umm works out of the box i think

    • @FloWoelki
      @FloWoelki  9 місяців тому +1

      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 9 місяців тому

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

    • @FloWoelki
      @FloWoelki  9 місяців тому +1

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

  • @tyrendel
    @tyrendel 2 місяці тому +1

    Rc section: Why did you use replace_with and clone the vec, rather than borrow_mut and push ?

    • @FloWoelki
      @FloWoelki  Місяць тому

      For this specific use case, the `borrow_mut` is definitely more efficient and clearer. The `replace_with` functionality was just used for educational purposes and to have some more general patterns. So, the `replace_with` could be, for example, used for temporarily taking ownership of the value behind the mutable reference and then applying a function that requires ownership.

  • @fernbe
    @fernbe 3 місяці тому

    Great valuable. This helps me understand

  • @yapayzeka
    @yapayzeka 2 місяці тому

    what is the point of using Mutex in a single thread environment?

  • @kingkolya9842
    @kingkolya9842 9 місяців тому +1

    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  8 місяців тому +2

      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());
      }
      ```

  • @iamwhoiam798
    @iamwhoiam798 Місяць тому +1

    omg, does he think RC checks for items and delete the vector when item count is zero ? 5:20

    • @FloWoelki
      @FloWoelki  Місяць тому

      Maybe it was hard to understand, but I didn't mean to say that. Rc doesn't "clear itself out" when items are gone. Instead, when all strong references to an Rc are dropped, the value it points to (which could be a container) is deallocated.

  • @eonraider
    @eonraider 2 місяці тому

    You have used the word "basically" at least 20 times during this video.

    • @FloWoelki
      @FloWoelki  2 місяці тому +2

      So sorry :D I have to get rid of it; it's some sort of filling word.

  • @KhodeCamp
    @KhodeCamp 8 місяців тому

    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  8 місяців тому +1

      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.