This vid is gold 🥇 it covers everything Info ✔️ Visual representation ✔️ No jokes ✔️ Step by step through errors ✔️ Covers pitfalls you might encounter ✔️ Different implementation ✔️
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.
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.
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.
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()); } ```
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
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.
Great explanation, thank you so much! After only 3 minutes of this video I clicked the subscribe button 😃👍
That's so awesome to hear! Glad you've liked it :)
This vid is gold 🥇 it covers everything
Info ✔️
Visual representation ✔️
No jokes ✔️
Step by step through errors ✔️
Covers pitfalls you might encounter ✔️
Different implementation ✔️
Thank you so much, really appreciate it :)
Rc section: Why did you use replace_with and clone the vec, rather than borrow_mut and push ?
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.
What plugin makes the errors appear right at the end of the line like that?
Error lens
Nvm its nvim umm works out of the box i think
exactly. you could try "Error lens" for vs code. for nvim, i personally use zero-lsp which also shows me the error inline.
@@xyangst Hmmm, I use neovim and it doesn't do that for me with lsp turned on.
you also need to enable the inline diagnostics (ref to this link: neovim.io/doc/user/diagnostic.html#vim.diagnostic.config()). hope that helps :)
Great valuable. This helps me understand
Awesome stuff :)
He is back! Nice to see you also switched to english. And that you work with rust ;-)
what is the point of using Mutex in a single thread environment?
omg, does he think RC checks for items and delete the vector when item count is zero ? 5:20
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.
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.
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());
}
```
You have used the word "basically" at least 20 times during this video.
So sorry :D I have to get rid of it; it's some sort of filling word.
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
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.