The Secret to Rust Ownership: Rc vs. Arc

แชร์
ฝัง

ความคิดเห็น • 25

  • @jeremietamburini
    @jeremietamburini หลายเดือนก่อน +1

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

    • @FloWoelki
      @FloWoelki  หลายเดือนก่อน

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

  • @lMINERl
    @lMINERl 4 หลายเดือนก่อน +4

    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 หลายเดือนก่อน +1

      Thank you so much, really appreciate it :)

  • @tyrendel
    @tyrendel 3 หลายเดือนก่อน +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.

  • @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 :)

  • @fernbe
    @fernbe 4 หลายเดือนก่อน

    Great valuable. This helps me understand

    • @FloWoelki
      @FloWoelki  4 หลายเดือนก่อน

      Awesome stuff :)

  • @codingcrashkurse6429
    @codingcrashkurse6429 9 หลายเดือนก่อน

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

  • @yapayzeka
    @yapayzeka 3 หลายเดือนก่อน

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

  • @iamwhoiam798
    @iamwhoiam798 2 หลายเดือนก่อน +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.

  • @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  9 หลายเดือนก่อน +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());
      }
      ```

  • @eonraider
    @eonraider 3 หลายเดือนก่อน

    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.