Rust Interior Mutability - Sneaking By The Borrow Checker

Rust Interior Mutability - Sneaking By The Borrow Checker

Code to the Moon

1 год назад

67,495 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

OgloTheNerd
OgloTheNerd - 26.10.2023 21:44

What makes you want to use MacOS instead of something like Linux?

Ответить
PETER GREEN
PETER GREEN - 11.10.2023 04:05

You reffer to Cell, Refcell, Mutex and RwLock as smart pointers, but they are not pointers. The value stored is stored as part of the Cell/Refcell/Mutex/Rwlock not in a seperate location on the heap.

Also while you can't use get on a cell containing a non-copy type, if your type implements Default (as for example String does) you can use take. So you can write code like.

let tmp = cell.take();
// do something with tmp.
cell.put(tmp);

Ответить
piraka_mistika
piraka_mistika - 25.09.2023 04:28

This was so helpful!

Ответить
Hadi Ariakia
Hadi Ariakia - 21.08.2023 06:50

Thanks a lot. Really appreciate your high quality videos

Ответить
VirusBLITZ
VirusBLITZ - 10.08.2023 12:13

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

Ответить
Vitor Nesello
Vitor Nesello - 24.07.2023 16:18

@codetothemoon Great video! And I loved you doom emacs setup, would you mind sharing your dotfiles?

Ответить
meowsqueak
meowsqueak - 15.07.2023 04:35

In the end, the Node struct ends up with various smart pointers on different fields. I understand we got there by construction, but I wonder if there’s a way to refactor it so that the smart pointers are just on Nodes, rather than the internal fields? Could the add_urgency() function somehow lock an entire node rather than the internal field? What would that look like?

Ответить
Logan Hodgson
Logan Hodgson - 11.07.2023 09:55

I like using cell for non-copy types too.
Doing `let mut contained = cell.replace(String::new());` allows you to take the string out of the cell, and it can be put back with `cell.replace(contained);`

Ответить
DAVID ALEJANDRO PINEDA
DAVID ALEJANDRO PINEDA - 07.07.2023 15:32

Can You give an use case of Cow Smart pointer?

Ответить
DAVID ALEJANDRO PINEDA
DAVID ALEJANDRO PINEDA - 07.07.2023 15:32

Thanks! Excelent content

Ответить
Nirmalya Sengupta
Nirmalya Sengupta - 23.06.2023 08:19

Short and sweet. Thank you!

Ответить
Thomas Andolf
Thomas Andolf - 06.06.2023 13:08

i have watcched so many videos about RefCell and Rc and what all tutorials fail on is that no one shows a real world problem applying Rc or RefCell. Every one builds these simple basic graphs that is not applicable on a real life problem. My suggestion to you is that in your tutorial you set out to solve a specific problem instead, that can only be solved by using interior mutability, instead of building this fictional simple graph. Just some feedback.

Ответить
Parlor 311
Parlor 311 - 27.05.2023 01:28

Yep, probably not going to become a Rust dev

Ответить
Dulanjala007
Dulanjala007 - 26.05.2023 19:06

but they are slower than if there's no unsafe code at all... there's performance hit even for the simplest cases just because we can't express things clear enough... or I'm wrong..

Ответить
Remco Greve
Remco Greve - 13.05.2023 10:54

At the time this videos made or because of Allan Turing?

Ответить
DAoC Nostalgia
DAoC Nostalgia - 26.04.2023 05:28

Good for me, but not for thee 😏

Ответить
Lapė Kalė
Lapė Kalė - 07.04.2023 20:13

Nice and crispy video just too fast for me

Ответить
Lucas Carvalho
Lucas Carvalho - 03.04.2023 03:16

How and why Arc<> is a costless abstraction?

Ответить
alterX2ego
alterX2ego - 01.04.2023 16:49

holly f this is complicated !!! :(

Ответить
Anony Mousse
Anony Mousse - 19.03.2023 01:12

Long story short, bad language design which was intended to "keep you safe" makes your job more verbose and annoying. I'd rather have competent programmers use my language than have to teach users with the compiler the basics of how a computer works. It should be do as you learn, not learn as you do.

Ответить
Pudicio
Pudicio - 16.03.2023 01:49

I can understand why people love this language but if I don't need the performance I prefer living with a GC. The hoops this throws are just crazy just to implement a simple graph structure..

Ответить
hupa1a
hupa1a - 11.03.2023 17:31

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

Ответить
paul gross
paul gross - 08.03.2023 01:36

Wonderful!

Ответить
Dionysis
Dionysis - 25.02.2023 17:01

Great content 🙂👍

Ответить
David Walton
David Walton - 20.02.2023 08:11

Great content, and I like your "no bullshit" style. Subscribed!

Ответить
Farbod Shahinfar
Farbod Shahinfar - 15.02.2023 20:43

You are awesome !

Ответить
René Ribaud
René Ribaud - 09.02.2023 21:29

Merci !

Ответить
René Ribaud
René Ribaud - 09.02.2023 21:27

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.

Ответить
Gabriel Nützi
Gabriel Nützi - 09.02.2023 00:32

I am just feeling wrong passing a non-mutable node but in the function change its members... Is that legit?? It seems like a codesmell

Ответить
Jeff G
Jeff G - 08.02.2023 05:37

If you're looking for topics, I think a nice related topic (regarding graphs) is IndraDB would be a great one. I never really had a need for a graph database yet, but I'm sure I will at some point, and would be cool to know an open source rust graph db.

Ответить
Jared Butcher
Jared Butcher - 05.02.2023 01:27

If I had seen this video existed a month ago, it would have saved me from spending a whole day banning my head into a wall.

Ответить
Alex Trotta
Alex Trotta - 04.02.2023 11:25

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.

Ответить
Panic Nao
Panic Nao - 04.02.2023 07:31

My interior mutability game has been pretty clumsy. This helped a lot. Perfect level of detail, and the end-to-end explanation of how the pieces relate was just what I needed to shore up my understanding. Thanks for doing what you do.

Ответить
Mario Rossi
Mario Rossi - 03.02.2023 09:34

Wow, very neat explanation! 👍

Ответить
modolief
modolief - 31.01.2023 11:20

Very very nicely done. Kudos.

Ответить
swapode
swapode - 30.01.2023 12:19

I think it's worth pointing out that interior mutability should be seen as kind of a last resort. I say kind of because obviously there are plenty of situations where it's the right choice - but frequently it's used as a crutch to use Rust just like you would a garbage collected language, when just a slight change in how we think about memory or control flow would lead to better results. That's probably the hardest part about really learning Rust, but the cool thing is that once you get the hang of it, it's easily applied to other languages as well and tends to lead to better project structure.

Ответить
Filippo De Grandi
Filippo De Grandi - 30.01.2023 11:39

Great video as always, I was wondering since I'm rewriting my neovim config from zero, what color scheme is this?

Ответить
Sutsuj
Sutsuj - 29.01.2023 17:59

One thing is that in rust you should first try to change your API, so that you don't need interior mutability, if that doesn't work only then should you consider using it.

Ответить
Shiny Jonny
Shiny Jonny - 28.01.2023 17:00

One of the greatest benefits of Rust is that it makes you uncomfortable using worse patterns.
Sure, you can implement it as a traditional graph, but you could also implement it as an entity component system with a vector of nodes and a vector of relationships between them.
That makes it:
1. Memory safe
2. Cache-friendly
3. Without the overhead of reference counting
I'm not saying people should not use RefCell, but people should ask themselves: Do I need it? Or do I want it because that is how it's done in C++/java/...? And is there a better pattern?

Ответить
drvanon
drvanon - 28.01.2023 11:45

One of your assumptions for the use of borrow_mut is incorrect: you assume that there would never be two borrow muts in your add one function, but the cycle case would create a borrow_mut for each loop of the cycle.

Ответить