Тёмный

Rust's Alien Data Types 👽 Box, Rc, Arc 

Code to the Moon
Подписаться 68 тыс.
Просмотров 134 тыс.
50% 1

Rust's smart pointers can be a bit confusing for developers coming from garbage collected languages. Let's walk through some very simple examples to understand when and how to use the most common ones.
00:00 Intro
00:32 Box
03:34 Rc
09:08 Arc
11:42 Outro
---
Stuff I use to make these videos - I absolutely love all of these products. Using these links is an easy way to support the channel, thank you so much if you do so!!!
Camera: Canon EOS R5 amzn.to/3CCrxzl
Monitor: Dell U4914DW 49in amzn.to/3MJV1jx
Lens: Sigma 24mm f/1.4 DG HSM Art for Canon EF amzn.to/3hZ10mz
SSD for Video Editing: VectoTech Rapid 8TB amzn.to/3hXz9TM
Microphone: Rode NT1-A amzn.to/3vWM4gL
Microphone Interface: Focusrite Clarett+ 2Pre amzn.to/3J5dy7S
Tripod: JOBY GorillaPod 5K amzn.to/3JaPxMA
Keyboard: Redragon Mechanical Gaming Keyboard amzn.to/3I1A7ZD
Mouse: Razer DeathAdder amzn.to/3J9fYCf
Computer: 2021 Macbook Pro amzn.to/3J7FXtW
Caffeine: High Brew Cold Brew Coffee amzn.to/3hXyx0q
More Caffeine: Monster Energy Juice, Pipeline Punch amzn.to/3Czmfox
Building A Second Brain book: amzn.to/3cIShWf

Наука

Опубликовано:

 

11 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 277   
@codetothemoon
@codetothemoon Год назад
ERRATA: 1. I mention that stack memory has faster access time than heap memory. While *allocating* and *deallocating* stack memory is much faster than doing so on the heap, it seems like access time for both types of memory is usually roughly the same.
@ateijelo
@ateijelo Год назад
I was just thinking about this at the beginning of the video. Heap and stack are just different areas of the same system memory. What matters here is that the stack is used to keep the "frame", i.e. all the values that are local, to the current function. This is how, after a function call returns, local variables retain their values, and this is what makes recursion possible. This stack behavior is implemented by keeping a pointer to the "top" of the stack and, on each function call, moving that pointer by an amount equal to the size of the new function's stack frame. That's why the compiler needs to know the size of the stack frame, and consequently, the size of any local variable to a function. Every other object that's dynamic in nature, or recursive, will have to live outside the stack, i.e. using Box. And like you just explained, deallocating on the stack is quite fast, since things aren't really "deallocated", the Stack Pointer is just moved back to where it was before the function call, while allocating and deallocating on the heap usually involves interacting with the Operating System to ask for available memory. Great video! Keep it up!
@oconnor663
@oconnor663 Год назад
I think "stack is faster than heap" is a pretty reasonable starting point, especially for a talk that isn't going into nitty gritty details about allocators and caching. Stack memory is pretty much guaranteed to be in your fastest cache, but with heap memory a lot depends on access patterns. If you have a really hot Vec then sure, there's probably no performance difference compared to an array on the stack. But for example a Vec where each String has its own heap pointer into some random page, isn't going to perform as well.
@Ruhrpottpatriot
@Ruhrpottpatriot Год назад
@@oconnor663 For most programmers that aren't going down the nitty-gritty sysprog hole the assumption that "stack is faster than heap" covers 95% of all use-cases. The msot time spent when dealing with memory is allocating and deallocating after all.
@phenanrithe
@phenanrithe Год назад
You'd need to set another register than EBP but the type of memory is indeed exactly the same, and the cache will cover both. But there may be system calls when using the heap. "In an ideal world you'd have everything on the stack" - I disagree if that's in the absolute, bear in mind the stack is limited in size and if you cannot often control what was stacked before your function is called or what will be stacked by the code called by your function. It's not appropriate for collections either because it would complicate size management and cause more memory moves (which are very power-consuming). But I think you meant it otherwise, for small objects in simple cases where this isn't a concern. These days memories are so large that people tend to forget about those limitations and then they are surprised the first time they have to deal with embedded code. ;-)
@LtdJorge
@LtdJorge Год назад
It makes total sense, both are in RAM. The thing is the stack is contiguous so writing to it is fast because the writes are sequential, while the heap is probably fragmented, which means random writes. Edit: without taking into account what the others have said, about frames, OS allocation, etc, everything contributes.
@miguelito0o
@miguelito0o Год назад
Sir, your Rust tutorial are cohesive, easy to follow ( due to great examples ) and don't go overly deep into the details. Perfect combination. Keep up with the good work.
@codetothemoon
@codetothemoon Год назад
Thanks for the kind words Miguel! It's thrilling to know that these videos can make these concepts a bit more palatable.
@ScarfFoxxy
@ScarfFoxxy 11 месяцев назад
​@codetothemoon, the way you described lifetimes just clicks
@cloudsquall88
@cloudsquall88 Год назад
Honestly, I 've read about these things 3-4 times, and I more or less understand them, but it really clicks differently when someone tells you "these are the two main uses of Box: unsized things and self-referencing structs". Thank you, this is really helpful!
@codetothemoon
@codetothemoon Год назад
Nice, I'm so glad you found that perspective valuable!
@WilderPoo
@WilderPoo Год назад
Stuff on Cell and RefCell would be exactly what I'm looking for, thanks for these great videos! 😄
@codetothemoon
@codetothemoon Год назад
Nice, I've put it on the video idea list!
@edgeeffect
@edgeeffect Год назад
As far as I can see, if your implementation requires RefCell then your implementation is probably wrong. ;)
@eboatwright_
@eboatwright_ Год назад
WOW WOW WOW! Rust is my favorite programming language, and I’ve used it for all sorts of things, but I’ve never dived into smart pointers (except box) and this was super helpful!
@codetothemoon
@codetothemoon Год назад
Nice, glad you found it valuable!
@fightndreamr
@fightndreamr Год назад
Thanks for the helpful video! It takes me a bit to catch everything on the first time around so I need repeat parts, but the clear examples and broken down explanation really help a lot.
@NikolajLepka
@NikolajLepka Год назад
It should be noted that in the Rc example, you could just have written truck_b.clone() instead of Rc::clone(truck_b)
@apffer
@apffer 8 месяцев назад
The rust book teaches like he did, Rc::clone(&an_rc), i think the reason is just to be idiomatic. Nice to know both ways are fine.
@Mustafa-099
@Mustafa-099 11 месяцев назад
This is sooo awesome!! I never understood the concept of Arc pointer until now, thank you so much :D
@codetothemoon
@codetothemoon 11 месяцев назад
thanks for the kind words, really happy you got something out of the video!
@cristobaljavier
@cristobaljavier Год назад
Great video, concise and well explained, just what I was looking for Rc. Please keep them coming.
@codetothemoon
@codetothemoon Год назад
Nice CJ! Glad you found it valuable - more to come!
@vuanh4084
@vuanh4084 Год назад
Your tutorial is very clear and easy to understand. Thank you so much. I hope you will create a video about RefCell soon.
@marcellerusu
@marcellerusu 47 минут назад
This was super informative, Rc finally clicked for me! Thank you!
@Mirusim
@Mirusim Год назад
I’m so glad that I found you channel. So easy to understand now
@cameronraw5906
@cameronraw5906 Год назад
Amazing help! Instantly subscribed.. I've been trying to figure out Dependency Injection in Rust and had no idea Rc is what I needed.
@gorudonu
@gorudonu Год назад
you're doing amazing work doing those videos! please keep going. it would be also cool to see ffi and unsafe rust
@codetothemoon
@codetothemoon Год назад
Thank you gorudonu! More on the way, and I've put FFI/unsafe on the video idea list.
@hv1461
@hv1461 Год назад
Your a great teacher. I would love videos where you develop small programs that illustrate various language features.
@hv1461
@hv1461 Год назад
It was very helpful to put forward usage scenarios.
@user-tt8fv5mj5y
@user-tt8fv5mj5y Год назад
You explained so clear for these complicated concepts~Thx!
@codetothemoon
@codetothemoon Год назад
Glad it was helpful!
@luxurycar8904
@luxurycar8904 5 месяцев назад
I love your videos. Thanks for taking the time to make these videos.
@ramkumarkb
@ramkumarkb Год назад
Great video! I finally understood smart pointers and its appropriate usecases 🎉
@codetothemoon
@codetothemoon Год назад
Thanks Ramkumar, so happy it helped you!
@i_am_feenster
@i_am_feenster Год назад
These are extremely nice video's, thank you!
@codetothemoon
@codetothemoon Год назад
Thanks and thanks for watching Jos!
@JamesHarrisonHaribo
@JamesHarrisonHaribo Год назад
Loved your video. There was some handy pointers in there 🥁. But absolutely would love to see a video covering RefCell
@codetothemoon
@codetothemoon Год назад
Haha! Seems like there is a lot of desire for RefCell, I've placed it high on the video idea list.
@fotisgimian4258
@fotisgimian4258 Год назад
Absolutely love your videos! Keep up the great work. 😍
@codetothemoon
@codetothemoon Год назад
Thanks so much for your support Fotis!
@ricardom860
@ricardom860 Месяц назад
Thanks for your great content!!
@NamasteProgramming
@NamasteProgramming Год назад
Your tutorials are clean, comparatively fast and easy to understand
@codetothemoon
@codetothemoon Год назад
Thanks Namaste (amazing name btw!), glad you found it valuable!
@azzamsya
@azzamsya Год назад
Thanks a ton for creating this! Can't wait for new rust videos.
@codetothemoon
@codetothemoon Год назад
Thanks for watching, more to come!
@poketopa1234
@poketopa1234 2 месяца назад
Such high quality videos. Thank you :)
@codetothemoon
@codetothemoon Месяц назад
thanks for watching!
@isheanesunigelmisi8400
@isheanesunigelmisi8400 Год назад
Welcome back
@codetothemoon
@codetothemoon Год назад
Thanks!
@Incertophile
@Incertophile Год назад
These videos are wonderful as someone new to the language. Thank you!
@codetothemoon
@codetothemoon Год назад
Great, that's precisely what I'm aiming for! Glad you found it valuable!
@almuaz
@almuaz 5 месяцев назад
I saw a lot of examples, including THE BOOK, and rust by examples, a lot of youtube videos. still didn't fully understand why how what. now i think i understood Rc finally. Thank you.
@na3aga
@na3aga 3 месяца назад
Also, to mention about Box usecases. The first use cases covers it, but it's not straightforward. Imagine that we are possibly returning many structs that implement the same trait from the function. In this case, the return type can not be known at compile time, so we need to make it Box
@macaco_agiota
@macaco_agiota Год назад
Wow. Amazing content!!!
@codetothemoon
@codetothemoon Год назад
thank you!! 😎
@vanish3408
@vanish3408 Год назад
Thanks for this video! These smart pointers are confusing. Could you also cover Cow in one of your next videos?
@codetothemoon
@codetothemoon Год назад
Seems like we have a few requests for Cow, I’ve added it to the video idea list!
@vanish3408
@vanish3408 Год назад
@@codetothemoon thanks!
@ianlogan3055
@ianlogan3055 Год назад
This video is great, thank you for making it.
@codetothemoon
@codetothemoon Год назад
Thanks for watching!
@gamcd
@gamcd Год назад
The quality of these videos is great, 60fps is a nice touch
@codetothemoon
@codetothemoon Год назад
Thanks Gavin! Impressed you noticed the 60fps ;)
@spinthma
@spinthma Год назад
Very good meta informations! Thank you
@codetothemoon
@codetothemoon Год назад
Thanks and thanks for watching!
@pablobellidoalva9521
@pablobellidoalva9521 Год назад
Thanks, just what I needed
@codetothemoon
@codetothemoon Год назад
glad it was helpful!
@chris360kss
@chris360kss Год назад
Very helpful thanks!
@codetothemoon
@codetothemoon Год назад
Glad you found it valuable, thanks for watching!
@aviral.rabbit
@aviral.rabbit 22 дня назад
great content!
@JannisAdmek
@JannisAdmek Год назад
Wow that's an excellent video!
@codetothemoon
@codetothemoon Год назад
thank you, glad you got something out of it!
@aviral.rabbit
@aviral.rabbit 22 дня назад
great video!
@nuElevenGG
@nuElevenGG Год назад
i'm liking the quick vids
@codetothemoon
@codetothemoon Год назад
glad to hear, thanks for watching!
@s1ck23
@s1ck23 Год назад
Great video! I think what would have been simpler to explain the difference between Rc and Arc without mentioning reordering, is that the increment and decrement of the internal strong and weak counters are represented as AtomicUsize in Arc (i.e. thread-safe) and usize (i.e. non-thread-safe) in Rc.
@codetothemoon
@codetothemoon Год назад
Thanks and thanks for the feedback! Touching on ordering was probably a little confusing, to your point I probably could have just mentioned the different counter types, and that one is thread safe while the other isn't
@InMemoryOfNeo
@InMemoryOfNeo Год назад
awesome video, thanks.
@codetothemoon
@codetothemoon Год назад
thanks, glad you liked it!
@banocean
@banocean Год назад
Literally best place to explain Box I found
@codetothemoon
@codetothemoon Год назад
nice, really happy that you found it valuable!
@sovrinfo
@sovrinfo Год назад
This video is great, thank you
@codetothemoon
@codetothemoon Год назад
Glad you found it valuable, thanks for watching!
@pacholoamit4408
@pacholoamit4408 Год назад
Just the vid I needed
@codetothemoon
@codetothemoon Год назад
nice, glad you found it valuable!
@ThorkilKowalski
@ThorkilKowalski Год назад
I like the pace of this video.
@codetothemoon
@codetothemoon Год назад
Thanks Thorkil, glad you liked it!
@jiaqingw
@jiaqingw 8 месяцев назад
best rust tutorial online, period
@codetothemoon
@codetothemoon 8 месяцев назад
thank you so much!
@christopherprobst-ranly960
@christopherprobst-ranly960 Месяц назад
The stack is not faster than heap. Both are locations in main memory. True, stack might be partially in registers, but in general, stack is no different to heap. Heap memory involves an allocator which in turn of course causes more overhead (internal some atomics need to be swapped and free memory has to be found). But stack and heap are both located in equally fast main memory.
@codetothemoon
@codetothemoon Месяц назад
I misspoke on this - thanks for pointing it out! I made a pinned comment about it.
@eengamer158
@eengamer158 Год назад
What about the RefCell? It is mentioned in the intro but never explained what it does
@codetothemoon
@codetothemoon Год назад
I excluded it from this video to keep things concise, and I wasn't convinced it would be useful for the vast majority of folks. But several people have requested I cover it, so I may at some point. In the meantime there is coverage of it in one of the later chapters of the Rust book.
@brandonj5557
@brandonj5557 Год назад
Good stuff, just came across Box today
@codetothemoon
@codetothemoon Год назад
Thanks Brandon!
@v0xl
@v0xl Год назад
btw mem::drop is in prelude so you can just use drop(...)
@codetothemoon
@codetothemoon Год назад
ohh nice thanks for the pointer (no pun intended) !
@huseyinsariyev2869
@huseyinsariyev2869 Год назад
production. Thanks again!
@codetothemoon
@codetothemoon Год назад
Thank you too!
@JulianGoddard
@JulianGoddard Год назад
Watched a bunch of videos before this and didn't really get it at all. Now I feel like I have a pretty good idea of how to use each
@codetothemoon
@codetothemoon Год назад
Julian - that's fantastic! It thrills me to make tough concepts more palatable.
@JDalmasca
@JDalmasca 11 месяцев назад
This was a super helpful primer on why/when to use these types! Would love to see more content building on it. I'm trying to form some internal decision tree for how to decide how long a given piece of data should live for. Going to go see if you have any videos on that topic right now... 😁
@codetothemoon
@codetothemoon 11 месяцев назад
great, really happy you got something out of the video! I don't have a video specifically on deciding how long a piece of data should live for, but "Rust Demystified" does cover lifetimes.
@misterkevin_rs4401
@misterkevin_rs4401 Год назад
Thanks!
@tsioryfitiavanaanhykrishna6992
You got a new subscriber !
@codetothemoon
@codetothemoon Год назад
Thanks Tsiory, very happy to have you onboard!
@Westernaut
@Westernaut Год назад
I am unsure whether one should practice both safe and bad programming. At least it is safe, I suppose. Specifically, I do not understand one of these clone examples when good programming might ask the instance to remain singleton, all the way through (both literally and figuratively). You show us how to do it, and you behave as if: awesome.
@codetothemoon
@codetothemoon Год назад
they are singletons - when we call clone on the Rc/Arc smart pointers, it's the pointer that's being cloned, not the underlying data
@Westernaut
@Westernaut Год назад
@@codetothemoon That you can do it is not the point.
@skytech2501
@skytech2501 7 месяцев назад
you are awesome!!
@codetothemoon
@codetothemoon 7 месяцев назад
thank you, glad you found the video valuable!
@sc0820
@sc0820 Год назад
help me a lot. Thx.
@houtamelocoding
@houtamelocoding Год назад
As a C# developer my understanding is that Rc basically turns structs into classes
@codetothemoon
@codetothemoon Год назад
How so? I thought C# uses garbage collection as opposed to reference counting?
@houtamelocoding
@houtamelocoding Год назад
@@codetothemoon I didn't mean on the memory allocation part, more so of how reference types work in C#
@shaurz
@shaurz Год назад
I wouldn't say stack memory is faster to access, just that the allocation and deallocation is faster. It might be a bit faster in certain conditions since it will stay in cache most of the time.
@codetothemoon
@codetothemoon Год назад
Got it! Yeah my understanding was that stack memory is more likely to be stored on the CPU cache - but maybe that's possible for the heap as well... Though I haven't actually benchmarked this, maybe I'll do that...
@KirillMavreshko
@KirillMavreshko Год назад
Ordinary variables could also be assigned by the compiler to CPU registers, which makes them as fast as they get. This doesn't happen to the heap-allocated variables.
@chris.davidoff
@chris.davidoff Год назад
@@codetothemoon Access is fastest when the data is "near" the recent access. Which is a part of why data oriented programming is so much faster. but I bet the methods of memory access have changed so much that what we are taught is not what is implemented in the most recent technology
@sashimisub8536
@sashimisub8536 Год назад
Finally a rust tutorial that clicks !
@codetothemoon
@codetothemoon Год назад
awesome, glad you got some value out of it!
@2Fast4Mellow
@2Fast4Mellow 2 месяца назад
Still fairly new to Rust. If a routine has a reference of a clones structure, can it be changed, or does it more like get a copy?
@modolief
@modolief Год назад
Omg, I _love_ your intro graphic, played at 0:30. *It's short!* Who wants to sit through 5 or ten seconds of some boring intro boilerplate every time we visit that channel, like a bad modal dialog box on some Windows 95 app, drives me nuts.
@codetothemoon
@codetothemoon Год назад
thanks modolief! I'd thought about creating a little intro reel, but every time I consider it I conclude that it would hinder my mission to provide as much value as possible in as little time as possible
@modolief
@modolief Год назад
@@codetothemoon The channel "PBS Eons" also has a really good intro bit. They start their video, then at around 20 or 30 seconds they give their little imprint. But what I really like about it is that even though it's more than about 3 seconds it fades out quickly, and they already start talking again before the sound is done. Very artistic, yet not intrusive.
@samwilson5544
@samwilson5544 Год назад
It's short, which I like, but the sound is kind of jarring.
@TheRealAfroRick
@TheRealAfroRick 8 месяцев назад
Was watching your Box part and was like... yep, I know those errors 😂😂😂
@codetothemoon
@codetothemoon 8 месяцев назад
they are a rite of passage every Rust developer must traverse.... 😎
@prasadsawool6670
@prasadsawool6670 Год назад
very nice video
@codetothemoon
@codetothemoon Год назад
thank you!
@anowerhosen9905
@anowerhosen9905 Год назад
Nice continue
@codetothemoon
@codetothemoon Год назад
Thanks I shall continue!
@ai-prendre
@ai-prendre 9 месяцев назад
Sir, what extension you use to have the UI Run in the main function.
@totalolage
@totalolage Год назад
Me (a frontend javascript webdev): fascinating!
@codetothemoon
@codetothemoon Год назад
nice, it seems like many JS frontend devs are interested in Rust!
@yuvraj7214
@yuvraj7214 Год назад
Hey man, I really like your VSCode theme, can you tell me which one are you using?
@codetothemoon
@codetothemoon Год назад
Sure it's Dark+!
@ZhdanovArtem
@ZhdanovArtem Год назад
@@codetothemoon Thanks! Have changed my theme.
@MrZiyak99
@MrZiyak99 Год назад
So in the RC example would the memory exist until the main function gets completed since it adds to the strong count?
@codetothemoon
@codetothemoon Год назад
that's correct! Rc doesn't really help much if you intend to hang on to one reference until the program ends - you could just use regular borrows in that case - but in this example to show the strong_count function I just kept a reference in main.
@toosafelol
@toosafelol Год назад
Good video and One RefCell pls.
@codetothemoon
@codetothemoon Год назад
Thanks, will do one eventually, wishing I had done it for Halloween as I think it has the appropriate level of spookiness 🎃
@ic6406
@ic6406 4 месяца назад
11:02 this what I don't rust for. Where did we pass truck_b ownership to the thread? I don't see any obvious code that tells me that truck_b moved to the thread. The variable of type Arc is cloned by readonly reference, so why it passes ownership?
@FaisalAhmed-xq8xq
@FaisalAhmed-xq8xq Год назад
Great video. What is this vscode theme?
@codetothemoon
@codetothemoon Год назад
Thanks and thanks for watching! VSCode theme is Dark+
@paoloposso
@paoloposso Год назад
Hey please create a video about refcell and cell!
@codetothemoon
@codetothemoon Год назад
Definitely doing this at some point, given the spooky factor it would have been a good one for halloween, but unfortunately it probably won't be ready in time 🎃
@allixender
@allixender Год назад
Yeah, please do RefCell as well. I'd also love you looking at Axum/Hyper/Tower ecosystem, or some of the popular data parallel computing libs.
@codetothemoon
@codetothemoon Год назад
I've added RefCell to the video idea list! I've been curious about those frameworks as well, especially Axum.
@bocckoka
@bocckoka Год назад
The stack and the heap are just as fast, because they are on the same system memory. What takes time is allocation and pointer dereferencing.
@bocckoka
@bocckoka Год назад
yeah, now I see the stickied comment
@cerulity32k
@cerulity32k 10 месяцев назад
One more thing. I'm assuming that for clarity, you used the explicit Arc::clone instead of the suffixed version. You can use .clone() on an Rc/Arc and it will clone the reference instead of the data.
@codetothemoon
@codetothemoon 10 месяцев назад
thanks for pointing this out - I should have mentioned this in the video if I didn't!
@NikolajLepka
@NikolajLepka Год назад
My fave is Cow; Clone on Write
@petermichaelgreen
@petermichaelgreen 8 месяцев назад
If you are going to cover refcell, you should surely also cover it's siblings, Cell, UnsafeCell, Mutex and RwLock.
@codetothemoon
@codetothemoon 8 месяцев назад
I have another video for all of these (except UnsafeCell) - check out “Rust Interior Mutability”
@SenorJuancii
@SenorJuancii Год назад
Nice
@codetothemoon
@codetothemoon Год назад
Thanks!
@noblenetdk
@noblenetdk Год назад
Could you demonstrate or explain Yeet? Love your eplanations
@codetothemoon
@codetothemoon Год назад
I had to look this up - is this what you're referring to? lol areweyeetyet.rs/
@noblenetdk
@noblenetdk Год назад
Sorry I misspelled. its Yew - gui for rust
@codetothemoon
@codetothemoon Год назад
@@noblenetdk Oh actually I already have a video about Yew - check out "Build A Rust Frontend" from earlier this year!
@spaghettiking653
@spaghettiking653 Год назад
I'm interested how Rc knows when data is going out of scope, or being dropped like you did. How is it aware that the memory is no longer accessible after a specific point without knowing where the objects are created in the program? How does the Rc know that there is a reference to truck_b in the main function, for example?
@codetothemoon
@codetothemoon Год назад
great question, in Rc's implementation of clone there is `self.inner().inc_strong();` which increments the strong reference counter. So it doesn't necessarily know where the references are, it just increments a counter each time one is created. Then in Rc's implementation of the Drop trait (which has a drop method that is invoked when the implementor goes out of scope) we have `self.inner().dec_strong();` then if `self.inner().strong() == 0 { /*code for cleaning up memory here */ }`
@spaghettiking653
@spaghettiking653 Год назад
@@codetothemoon Ohh I see :)) Thanks very much, that makes sense!
@salihyarc7142
@salihyarc7142 Год назад
Whenever i use the GMS and put it in the soft, it holds out the note forever! please help, i am very confused
@codetothemoon
@codetothemoon Год назад
🔥
@ahuman32478
@ahuman32478 10 месяцев назад
What about the Cow type? Still struggle with that, even when I have the documentation open
@codetothemoon
@codetothemoon 10 месяцев назад
been meaning to make a video about it! stay tuned...
@raconvid6521
@raconvid6521 3 месяца назад
are Rc’s safe? How do they prevent immortal reference loops?
@bananaboye3759
@bananaboye3759 Год назад
Why is "recursive without indirection" an error? (3:00 ish)
@alwin5995
@alwin5995 Год назад
Love rust 💕
@codetothemoon
@codetothemoon Год назад
me too!
@murugarajuperumalla5508
@murugarajuperumalla5508 Год назад
Awesome, would like to see video on RefCell
@codetothemoon
@codetothemoon Год назад
I've put it on the video idea list!
@pup4301
@pup4301 Год назад
Less goooooooooooooo!!!!!!
@codetothemoon
@codetothemoon Год назад
🚀🌕
@kranfix
@kranfix Год назад
God video!
@codetothemoon
@codetothemoon Год назад
Haha thanks!
@peterthecoderd.1210
@peterthecoderd.1210 Год назад
This is timely for me. I ran into Rc and cell last night while trying to learn rust with GTK. I find it all very confusing. Anything you can provide including RefCell is greatly appreciated. Thanks.
@strangeWaters
@strangeWaters Год назад
It's a single-threaded mutex (well, read/write lock.) This might seem useless, but it can be used to create shared references that can still be modified: make an Rc, which you can clone freely, but you can still lock it for mutable writing. (if you try to take multiple write locks at the same time, the thread will panic.) it's sort of like a pointer to an object in a regular OO language. You can also use it to make mutable thread-local data. Keep in mind anything containing a refcell can't be sent across threads. They're also a pain to serialize.
@strangeWaters
@strangeWaters Год назад
sorry-- RefCells can be sent but references to them can't be sent, and Rcs / references to rcs can't be sent.
@codetothemoon
@codetothemoon Год назад
RefCell seems to be frequently requested, I'll probably make a video about it! In the meantime it looks like like strangeWaters has a good description, and there is also an explanation in chapter 15 of the Rust book.
@stephenJpollei
@stephenJpollei Год назад
For atomic, it is more than just compiler has to forgo some optimizations but it has to tell CPU to also not reorder, lock the bus, and handle cache-coherency issues. Both an INCrement and a DECrement, really have three parts load/read, compute, and store/write. Normally, both the compiler and the cpu can reorder many things and be lazy. So if you had pseudo-code: y=sin(x); if (cond) {i++}; pritnf("%d ",i); then compiler could reorder it to asm(pseudo x86): mov %eax, [i] mov %ebx, [cond] fsin x jz %ebx, prnt_label inc %eax prnt_label: push %eax push "%d" call printf mov [i],%eax We can have a lot going on between mov %eax, [i] (LOAD) and mov [i],%eax (STORE). The compiler needs combine mov %eax, [i], inc %eax, mov [i],%eax into : inc [i] .... But it also has to go further and add lock prefix . The lock prefix tells CPU that it has to make sure to hold the bus during the whole LOAD/COMPUTE/STORE phases of the instruction so another CPU doesn't do anything in the middle of all this. Also it has to make sure if other CPUs have L1, L2, etc cache that references that memory that it gets invalidated. c9x.me/x86/html/file_module_x86_id_159.html
@seannewell397
@seannewell397 Год назад
Woah
@seannewell397
@seannewell397 Год назад
Synchronization is expensive. Complexity in the code, complexity in the instructions, complexities in the CPU itself.
@mattidragon835
@mattidragon835 Год назад
How is cyclic data handled by Rc? As we can mutate the data we can give the value of the Rc a clone, right? Thus causing the data to never be deallocated
@jadpole
@jadpole Год назад
It isn't. You can define circular data with Rc that will never be deallocated. It's the programmer's job to handle this case correctly. This was actually at the centre of the Leakpocalypse. It was decided that, while accessing deallocated memory is `unsafe`, leaking memory isn't. You can somewhat get around this with weak references, to get circular data with deallocation, but it gets complicated pretty quickly.
@nurmohammed9642
@nurmohammed9642 Год назад
Hmm... Interesting, Maybe there would no cost for accessing variable that stored on heap, But rather there is a cost for allocation.
@codetothemoon
@codetothemoon Год назад
Yeah, I definitely appreciate that stack vs heap is much more nuanced than I made it out to be in this video...
@willi1978
@willi1978 Год назад
now i understand what people mean when they say the learning curve of rust is steep
@hv1461
@hv1461 Год назад
It’s really challenging. But so interesting. And as I learn Rust I feel as though I am learning very important concepts that are key to becoming a proficient software engineer.
@OliverUnderTheMoon
@OliverUnderTheMoon Год назад
4:10 Truck structure... struckture
@codetothemoon
@codetothemoon Год назад
lol nice!
@yato3335
@yato3335 Год назад
Why do you write Rc::clone() explicitely, instead of truck.clone() ?
@fdwr
@fdwr Год назад
🤔 I would understand them more intuitively if they were named more intuitively and consistently. One is a single ownership pointer, uniquely owned. One is a shared ownership pointer, implemented via reference counting. Another is the same as the previous, just with interlocked atomic increment/decrement. Names like "Box" and "Arc" though feel pulled out of a hat. A box has height, width, and depth, but there is nothing volumetric in Rust's "Box" (and loosely co-opting the concept of "boxing" from C# feels weird here).
@lycanthoss
@lycanthoss Год назад
Rc stands for reference counter and Arc stands for atomic reference counter, they are just abbreviations which is good because they are frequently used and imagine writing ReferenceCounter every time, especially when you have to wrap many things with them. For box it could be named better maybe, but there is no type that is going to be called a "box". If it is a math library it would call it cuboid, cube, rectangular prism or something else. For types that are frequently used short names are good.
@codetothemoon
@codetothemoon Год назад
Totally understand your frustration - to add to the other response, I believe "Box" and "Boxing" are terms that have histories that extend well prior to the inception of Rust, but are usually hidden from the developer by developer-facing language abstractions. I think Rust is just one of the first to actually expose the term directly to the developer.
@0LoneTech
@0LoneTech 9 месяцев назад
​​@@codetothemoon Example dated usage: X.Leroy. Unboxed objects and polymorphic typing, 1992. The terms have been used in libraries also, at least since 2007 in Haskell and 2000 in Steel Bank Common Lisp. I suspect it could be traced back several decades more.
Далее
Mocking Rust 🤪 and Testing 🧪
11:58
Просмотров 36 тыс.
Rust Demystified 🪄 Simplifying The Toughest Parts
14:05
#kikakim
00:11
Просмотров 3,2 млн
Creepy Teacher Kidnapped My Girlfriend?!
00:42
Просмотров 12 млн
Use Arc Instead of Vec
15:21
Просмотров 134 тыс.
Arc instead of Vec? | Prime Reacts
37:18
Просмотров 61 тыс.
but what is 'a lifetime?
12:20
Просмотров 58 тыс.
The Secret to Rust Ownership: Rc vs. Arc
13:47
Просмотров 1,2 тыс.
Self-referential structs (in Rust)
27:21
Просмотров 51 тыс.
Constructors Are Broken
18:16
Просмотров 99 тыс.
Избранное печатает...
0:11
Просмотров 119 тыс.
Мечта Каждого Геймера
0:59
Просмотров 322 тыс.
Mem VPN - в Apple Store
0:30
Просмотров 33 тыс.