The Secret to Rust Ownership: Rc vs. Arc

Поделиться
HTML-код
  • Опубликовано: 1 дек 2024

Комментарии • 26

  • @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 5 месяцев назад +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  5 месяцев назад +1

      Thank you so much, really appreciate it :)

  • @jay.rhoden
    @jay.rhoden 10 месяцев назад +2

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

    • @xyangst
      @xyangst 10 месяцев назад +1

      Error lens

    • @xyangst
      @xyangst 10 месяцев назад

      Nvm its nvim umm works out of the box i think

    • @FloWoelki
      @FloWoelki  10 месяцев назад +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 10 месяцев назад

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

    • @FloWoelki
      @FloWoelki  10 месяцев назад +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 3 месяца назад +1

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

    • @FloWoelki
      @FloWoelki  2 месяца назад

      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.

  • @yapayzeka
    @yapayzeka 3 месяца назад

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

  • @codingcrashkurse6429
    @codingcrashkurse6429 10 месяцев назад

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

  • @fernbe
    @fernbe 4 месяца назад

    Great valuable. This helps me understand

    • @FloWoelki
      @FloWoelki  4 месяца назад

      Awesome stuff :)

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

  • @ИванРагозин-я8я
    @ИванРагозин-я8я 6 дней назад

    lol, why don't you use Arc::clone() along with Arc:new()?

  • @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  2 месяца назад

      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 3 месяца назад

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

    • @FloWoelki
      @FloWoelki  3 месяца назад +2

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

  • @KhodeCamp
    @KhodeCamp 9 месяцев назад

    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  9 месяцев назад +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.