Rust Interior Mutability - Sneaking By The Borrow Checker

preview_player
Показать описание
The limitations of the Rust borrow checker and how to work around them with interior mutability. Cell, RefCell, RwLock and Mutex are all covered.

Stuff I use to make these videos:
Рекомендации по теме
Комментарии
Автор

I would love to watch some in-depth analysis on what currently is not feasible for a borrow-checker to accept but that is memory safe. A breakdown of various types of situations, which are likely to be fixable sooner, which later, and which perhaps never.

andrewdunbar
Автор

I encountered some these before but had trouble following the differences between them and I didn't get why I would use one over another, now that I've heard them all explained at once it makes much more sense. Thanks!

-ge
Автор

Because I'll keep coming back to this, here are some timestamps (please pin this!):

0:00 - Intro
1:31 - The Problem
5:34 - Cell (Cloning Available)
8:00 - RefCell (No Cloning)
10:37 - RwLock (Multithreading R/W Locks)
14:34 - Mutex (Multithreading Single Lock)
15:46 - Outro

TheRealBrenny
Автор

This is what I needed to try and implement the Monte Carlo tree search in Rust, thank you so much for the clear explanation

IqweoR
Автор

Very helpful video. I've been trying to port a simple simulation "game" with rather complicated data structures to Rust. I generally use this as a test to learn features of a new language. I originally wrote it on my Commodore 64 in BASIC (yes, I'm old, lol), then ported it to 6502 assembly, later C, C++, Java, and Python. Now I'm trying to port it to learn Rust and the borrow checker is giving me hell. I'm on my 6th refactor and I still don't have it working... I think the info from this video may help, though!

rancidbeef
Автор

7:10 I would usually write

for &adj in &node.adjacent

or whatever variation makes the borrow checker and type checker happy (the &adj is a pattern match, so you can use just adj both in the for declaration and the function call in the line below if that suits your needs, and I assume clippy will tell you as much).

That's what I did everywhere in my Advent of Code. That's also what your error message actually recommends.

MasterHigure
Автор

ngl this video is a much better introduction to interior mutability than the rust book

밤고
Автор

Nice! I’d also be interested in a crust-of-rust-but-faster style overview of how Cell/RefCell work internally, if there’s any insights to be gained there.

clo
Автор

Using the ' character for lifetime annotations is horrid and always unsettling to look at and parse with my eyes, and annoying to implement for editors/web views with lightweight syntax highlighting. Even writing it here in a post I'm worried about clarity and if it's hard to read. Rust should have used another keyword or @ or something, why is that operator so off-limits... It's not just an aesthetic problem, using ' actually causes language problems. For example it has ambiguity with trying to use const generic parameters of type char like if you wanted to use a type Foo<'a'> without some special-case scenario. Didn't Rust learn from C++ about doing crazy stuff with parsing special corner case syntaxes? It's bad like the problems C++ inherited.

zyxyuv
Автор

This amazing explanation deserves my subscription. Great job 👍

aneurysmjs
Автор

Clearest explanation I've seen so far. Would have taken 2 minutes to write in c++

poketopa
Автор

I enjoyed this video. I had some trouble understanding Cell and Refcell, and this video is beneficial to clarify these concepts. I think the example you have chosen is excellent because simple enough to focus on the concepts and not be bothered understanding a complex code. The explanations are also really clear, so thanks for sharing this content. That is an excellent job.

reneribaud
Автор

Again, a really great video! Can I request a video about lifetimes, specifically knowing what to add when rust tells you it needs them.

narwodev
Автор

Very helpful video! I think this will come in handy with converting some C++ code to Rust, as it might be safe but just harder to express with the borrow checker.

alextrotta
Автор

I realise that you're presenting a general case.
I've used the Atomic classes for simple types (bool, ints, floats) which are no bigger than my architecture, as they have ideal performance when you call them with relaxed timing.
When you enable any optimisation, it optimises down to the bare minimal instructions for assigning the data type.

sparkyj
Автор

You are so good at explaining these! Your rust content is the best out there! <3

virusblitz
Автор

By far the best explanation I've found, and the only one I've understood!

Christobanistan
Автор

Wow! This is so informative and easy to follow, although it's a complex topic for beginners like me! Great job

hupaa
Автор

Going to try to implement some of this info into a doubly linked list, thanks!

Drama-cktp
Автор

Very good and concise intro into that important Rust topic, thanks!

flippert
welcome to shbcf.ru