Visualizing memory layout of Rust's data types [See description/first comment]

UPDATE: This video is re-uploaded with a better audio here • Visualizing memory lay...
Covers how a binary is executed, what segments are mapped to memory, the purpose/working of stack and heap memory, and how values of Rust's data types are laid out in memory. The data types that we cover here are integers, char, Vector, slice, String, string slice, structs, enums, smart pointers like Box, Rc, Arc, Trait object and Fn traits like FnOnce, FnMut and Fn.
We also cover the difference between String and str types, Vector and slice types. Topics like fat pointers, optimizations done by the Rust compiler when you wrap a reference inside an Option are also covered.
0:00 Segments in a binary
3:14 Stack & Heap
5:46 CPU Word size
7:08 Working of stack memory
10:25 working of Heap memory
14:00 Integer data types
14:30 char
15:15 size_of & align_of
15:51 Reference
16:58 Array
17:21 Vector
18:19 Slice of T
19:54 String, str, &str
21:17 Struct
22:14 Enum
24:14 Box pointer
24:51 Optimizations on Option
26:34 Copy Vs Move
29:21 Rc pointer
30:45 Send vs Sync traits
31:00 Why RC is neither Send nor Sync
31:24 Arc
32:19 Trait object
34:20 Function pointer
34:37 Closures
Life of an async fn: • RustFest Barcelona - T...
stack & heap / virtual address space:
lwn.net/Articles/738975/
docs.microsoft.com/en-us/wind...
Book: Programming Rust by Jason Orendorff, Jim Blandy, and Leonora F.S. Tindall

Пікірлер: 180

  • @DenisFalqueto
    @DenisFalqueto2 жыл бұрын

    This solved lots of misunderstandings I had about Rust. It should be placed right in the documentation of the language (at least a link, so one could find it easily and early on the rust book)

  • @MrEnsiferum77

    @MrEnsiferum77

    Жыл бұрын

    Maybe the ownership is different, but everything else is school thing explained here.

  • @dreastonbikrain1896

    @dreastonbikrain1896

    Жыл бұрын

    @@MrEnsiferum77 could you point me to a program or a book where theese things are taught to people?

  • @snwbrdr36
    @snwbrdr362 жыл бұрын

    This was extremely enlightening in visualizing rust's memory model. Thank you for taking the time to create this!

  • @aqua3418
    @aqua34182 жыл бұрын

    Just remember about the memory layout of things like structs. You CANNOT rely on the memory layout or ordering of fields to be a certain way, as it's not defined how it will end up. If you need to rely on this, you need to use things like `#[repr(C)]`. In fact, not even the tuple struct's memory layout is guaranteed. To guarantee it, you would need to use `#[repr(transparent)]`

  • @Ma1ne2
    @Ma1ne2 Жыл бұрын

    This is not just a great video for Rust developers, but a great introduction for all developers into the process' memory layout and workings of the operating system!

  • @notsojharedtroll23

    @notsojharedtroll23

    Жыл бұрын

    Fax

  • @sreekanthpr
    @sreekanthpr2 жыл бұрын

    *UPDATE: This video is re-uploaded with a better audio here* kzread.info/dash/bejne/aZOjj7uLqMnFk8Y.html Corrections: * 6:10, it should be 2^64 - 1 (without parenthesis) * Trait is dynamically sized. Trait object has known size.

  • @dersebastianbauer

    @dersebastianbauer

    2 жыл бұрын

    28:22 this isn't precise. Gargabe collection (as in JS, Go or Java) does not keep track of refernces. In its cleanup run GC just checks wether there is a reference to this memory or not and after it is finished it forgets about it. Your description fits more to automatic reference counting used by Objective-C and Swift.

  • @shumudu5377

    @shumudu5377

    2 жыл бұрын

    by which software are the pictures drawn?

  • @sreekanthpr

    @sreekanthpr

    2 жыл бұрын

    @@shumudu5377 For this video, I used Powerpoint. For all other videos in this channel, I use manim library (Python).

  • @parry84

    @parry84

    2 жыл бұрын

    at 28:10, I suppose the indices should be 0, 1, 2 instead of 0, 2, 3. btw great video, thanks you ❤

  • @noop9770

    @noop9770

    Жыл бұрын

    * trait objects are dynamically sized types, meaning that their size is known at runtime. Pointer types to trait objects are sized at compile-time but have twice the size of pointers to sized types. Correct me if i got something wrong

  • @joelimbergamo639
    @joelimbergamo6392 жыл бұрын

    Incredible explanation, it makes it so much easier to understand compiler errors when you have this video and memory mapping in mind. Thanks !

  • @slava_in3757
    @slava_in3757 Жыл бұрын

    I was looking for info on dynamically sized types, but I found so much more in the video. One of the clearest explanations for such confusing concepts in Rust. Great video!

  • @sanket1729
    @sanket1729 Жыл бұрын

    Wow, I really love this. Nice refresher of multiple topics even for someone who is intermediate-advanced rust user.

  • @juanma3333
    @juanma33332 жыл бұрын

    This is awesome! I didn't know that I needed this that badly! Thanks for sharing this great work!

  • @norberthajagos
    @norberthajagos11 ай бұрын

    Extremly well delivered information with not a single wasted second. Very good work!

  • @GeorgeFosberry
    @GeorgeFosberry Жыл бұрын

    A nice overview and/or refresher.

  • @rajesh_404
    @rajesh_404 Жыл бұрын

    Hats off for making everything clear🙌 You're extremely talented. You're able to explain things logically by providing reasons. Thanks❤️

  • @bugsenthil
    @bugsenthil5 ай бұрын

    The best video on vtable , Smart pointers, trait object..Awesome job. Thanks

  • @360nickx
    @360nickx2 жыл бұрын

    Thanks for this. It is brilliant!

  • @adammontgomery7980
    @adammontgomery79802 жыл бұрын

    I was confused about box, rc, and arc until now. Thanks!

  • @ClearerThanMud
    @ClearerThanMud2 жыл бұрын

    Nice work! Hope to see more Rust videos from you.

  • @procyonq
    @procyonq Жыл бұрын

    Definitely the best Rust video on KZread. I will keep this video and watch again and again! 감사합니다🙏

  • @rajesh_404
    @rajesh_404 Жыл бұрын

    This video will remain as the best go-to video for new rustaceans! Excellent work Sreekanth!!!

  • @MasterSpencer2483
    @MasterSpencer24834 ай бұрын

    Genuinely, thank you SO much for this video. I was banging my head against a wall trying to PRECISELY understand fat pointers, why str is a primitive type, and other low-level questions

  • @tuanlam999
    @tuanlam999 Жыл бұрын

    Great explanation. Love to see more such deep dive video about Rust.

  • @jonathanmoore5619
    @jonathanmoore56192 жыл бұрын

    Rust is the way forward and this is a great piece of work. Thanks.

  • @marcoantonio7648
    @marcoantonio7648 Жыл бұрын

    This video is gold. Thanks for the hard work!

  • @user-ow5mn6dn7n
    @user-ow5mn6dn7n2 жыл бұрын

    Dude, amazing work! Just amazing

  • @jeffvandyke1239
    @jeffvandyke12392 жыл бұрын

    Beautiful, I'd consider the topics covered here to be required knowledge, and the video is a nice way to present things that can be difficult to visualize.

  • @mohitpawar8582
    @mohitpawar85822 жыл бұрын

    Very useful and nice depiction of memory layout of each data type. It should be placed right inside the documentation

  • @ikhlasulkamal5245
    @ikhlasulkamal5245 Жыл бұрын

    This is very clear and great explanation on rust memory management. thanks for the explanations

  • @askhatarslanov6327
    @askhatarslanov6327 Жыл бұрын

    finally I've understood strings in rust. thank you a lot!

  • @Leonardo-ms6re
    @Leonardo-ms6re2 жыл бұрын

    This is gold! Congrats :)

  • @shreyashsaitwal4244
    @shreyashsaitwal4244 Жыл бұрын

    This is gold! Thank you.

  • @malharvora1281
    @malharvora12812 жыл бұрын

    Easy to understand video for complex concepts. Thanks for sharing.

  • @malharvora1281

    @malharvora1281

    2 жыл бұрын

    It would be awesome if you put more Rust-related videos with the same level of explanation. IMHO there are a lot of videos of Rust internals available on KZread but only a few like this, are actually understandable by novice Rustaceans.

  • @JoaoBosco89
    @JoaoBosco89 Жыл бұрын

    Your video helped me a lot. Thank you.

  • @accumulator4825
    @accumulator48256 күн бұрын

    Really appreciate this video, thank you

  • @sokolorlov996
    @sokolorlov9969 ай бұрын

    Visualization helps a lot, thanks!

  • @Nre11
    @Nre112 жыл бұрын

    Wow, honestly incredible

  • @b5eu6b87
    @b5eu6b872 жыл бұрын

    extremely well done video!

  • @leonlee9518
    @leonlee95189 ай бұрын

    This video help me understand rust memory a lot , thanks ~

  • @mustafaerdemkoc2529
    @mustafaerdemkoc25292 жыл бұрын

    Thank you! this was very helpful.

  • @ancientITguy
    @ancientITguy2 жыл бұрын

    Great explanation! Thanks

  • @ozanmuyes
    @ozanmuyes2 жыл бұрын

    This is golden, thanks!

  • @m50505
    @m50505 Жыл бұрын

    thank you for sharing knowledge. appreciate the effort.

  • @sanjusk4287
    @sanjusk42872 жыл бұрын

    Such wisdom, oh great one.

  • @soapbubblesx
    @soapbubblesx2 жыл бұрын

    Enlightening! Thank you!

  • @gneryze
    @gneryze2 ай бұрын

    damn i cannot imagine its the day that i will truly understand how memory handled by kernel and OS, just by clicking video about RUST HAHHAA

  • @xyve6129
    @xyve61298 ай бұрын

    Great video. I wanted to check out Rust for use in userspace applications, however the types seemed too high level and convoluted to me, since I primarily program in C. This video helped visualize the types and allows me to think of them in a more familiar C like way and visualize them in memory. I feel like every languages needs videos like this, as it makes mechanisms within the language more explicit and easy to understand in simpler terms.

  • @young_kido2293
    @young_kido2293 Жыл бұрын

    Thank you man for sharing this stuff

  • @arash2229
    @arash2229 Жыл бұрын

    bruh I loved this video. thanks for your great content.

  • @mokorra
    @mokorra10 ай бұрын

    very informative! thank you

  • @user-io4sr7vg1v
    @user-io4sr7vg1v3 ай бұрын

    Wonderful. So thorough.

  • @Onkoe
    @Onkoe Жыл бұрын

    Great lecture, but in the future, consider asking a friend to narrate! I know a lot of people don’t like posting themselves online, but many people would probably be happy to help out! :)

  • @chudchadanstud
    @chudchadanstud8 ай бұрын

    Thank a lot, this makes learning Rust a lot easier. The less magic the better.

  • @RAHUDAS
    @RAHUDAS2 жыл бұрын

    great work, very helpful for me

  • @snk-js
    @snk-js2 жыл бұрын

    this is the best video I have ever watched on youtube

  • @user-rs4sg2tz6k
    @user-rs4sg2tz6k Жыл бұрын

    Jesus Christ. This is so awesome explanation. Thanks to you I can be now understanding how stack and heap grows up

  • @tanuvishu
    @tanuvishu2 жыл бұрын

    Awesome explaination

  • @tadeohepperle
    @tadeohepperle Жыл бұрын

    This is so useful! Thanks

  • @morgomi
    @morgomi Жыл бұрын

    the best introduction has ever seen.

  • @HansFlamme
    @HansFlamme2 жыл бұрын

    I recently started with rust and holy shit i can'ttell how helpful this was

  • @isaaccespedes4817
    @isaaccespedes481710 ай бұрын

    Thank you Sreekanth. Coming from a C background, this really helped me understand what Rust is about.

  • @dk5146
    @dk5146 Жыл бұрын

    need more videos like this

  • @InMemoryOfNeo
    @InMemoryOfNeo10 ай бұрын

    awesome explanation 👏👏👏👏👏

  • @vmscode
    @vmscode Жыл бұрын

    Amazing video! Would be awesome if you made more videos like this for Rust

  • @akatsukinoh7660
    @akatsukinoh7660 Жыл бұрын

    This is exactly i want. Thank you!

  • @michaelmueller9635
    @michaelmueller9635 Жыл бұрын

    This is high quality stuff!

  • @jokerdevilarm
    @jokerdevilarm2 жыл бұрын

    The best video!!! Thnx

  • @dionysis_
    @dionysis_2 жыл бұрын

    That was very useful 👍

  • @parthivmangukiya5953
    @parthivmangukiya59532 жыл бұрын

    Thanks!!! Your video is too good

  • @brethagen7776
    @brethagen77766 ай бұрын

    This video is beautiful

  • @abteyeb7969
    @abteyeb79692 жыл бұрын

    very good and clear

  • @georgereasonproductions
    @georgereasonproductions2 жыл бұрын

    love to learn thanks for sharing

  • @nurmohammed9642
    @nurmohammed96422 жыл бұрын

    We rustacean loved it! you should create more videos about rust, like this one!

  • @Policritics
    @Policritics2 жыл бұрын

    This is the best explain to Rust

  • @ingmarfalk3306
    @ingmarfalk33066 ай бұрын

    Bro this was so cool, loved the video even tho it was with an AI voice, usually dont like those but the content was NOICE

  • @MrSasukeUchihaEMS
    @MrSasukeUchihaEMS2 жыл бұрын

    good stuff, thank you very much

  • @reneribaud8912
    @reneribaud89124 ай бұрын

    Amazing video thanks.

  • @xl0xl0xl0
    @xl0xl0xl0 Жыл бұрын

    This makes so much more sense! Could you please cover RefCell?

  • @nav-on
    @nav-on Жыл бұрын

    love it help me a lot i hope you do cargo next!

  • @user-vi2jw2rc9u
    @user-vi2jw2rc9u2 жыл бұрын

    thank you for this

  • @scorpo999
    @scorpo999 Жыл бұрын

    This is worth more than some of computer arch classes at university.👍

  • @maximus1172
    @maximus1172 Жыл бұрын

    I cannot thank you enough 🙏

  • @user-xg9wt3he9q
    @user-xg9wt3he9q2 жыл бұрын

    Super! Thanks

  • @yapayzeka
    @yapayzeka Жыл бұрын

    this is the best memory video ever made. I downloaded it because of fear to lose it if youtube servers shut down or volcano, earthquake, meteor or something... lifesaving enum memory tip @23:55

  • @RivoMPutra
    @RivoMPutra Жыл бұрын

    just beca a subscriber keep up the good work Sir.

  • @aaryanpashine6633
    @aaryanpashine663310 ай бұрын

    holy shit this was amazing

  • @xenocampanoli815
    @xenocampanoli8158 ай бұрын

    The mention of "malloc" made me think of the assertions around self-compiled language aspects, something I first remember hearing about with Pascal, then C. I presume then this does not include the libc and some other libraries, or perhaps just not always. I guess if it is a clang compilation, by implication, it's not self-compiled, so perhaps the assertion is it is self-compiled in some environments, or that some copies are self-compiled, or that the core of the compiler is self-compiled, but not all the libraries it uses.

  • @deepmehta3840
    @deepmehta38402 жыл бұрын

    you are brilliant ser :)))))))))))))

  • @urbex9797
    @urbex97972 жыл бұрын

    24:30 The malloc implementation will have some overhead (at least 8 bytes) in managing the extra heap allocation.

  • @wuoyun6302
    @wuoyun63022 жыл бұрын

    thanks

  • @uncleyour3994
    @uncleyour3994 Жыл бұрын

    awesome, cool

  • @hariangr
    @hariangr2 жыл бұрын

    so freakin good

  • @happilyconfuseddog8951
    @happilyconfuseddog8951 Жыл бұрын

    Great ty ty

  • @alexloktionoff6833
    @alexloktionoff68337 ай бұрын

    Is it possible to make borrow-checker to take care of individual structure fields? I mean I want to pass the whole structure mutable reference in/out for functions, but to make the borrow-checker to care about what fields has been modified. Is it?

  • @nbes6328
    @nbes63282 жыл бұрын

    Awesome

  • @lavienrozy1867
    @lavienrozy18672 жыл бұрын

    O Captain! My Captain! Are you going to publish a book? I think we need more lectures for memory dynamics.

  • @JasonFritcher
    @JasonFritcher2 жыл бұрын

    Great video, thanks! At 6:10, there is an error for the address range. The caption reads 2^(64-1), which is incorrect, as it implies 2^63 because of the parenthesis. The parens either needs to be removed, or put around the exponent to be correct.

  • @sreekanthpr

    @sreekanthpr

    2 жыл бұрын

    Thank you. Happened during the final subtitle edits. It was written in words for generating the sound in the original text.

  • @chalmagean
    @chalmagean2 жыл бұрын

    Is this documented somewhere? Where did you find the info to make the video? Awesome video by the way.

  • @sreekanthpr

    @sreekanthpr

    2 жыл бұрын

    Rust specific things are mostly from the book "Programming Rust" (mentioned in video description)

  • @10e999
    @10e9992 жыл бұрын

    Great watch so far. Questions @8:45 shouldn't the main stack frame allocate the `b` variable before entering the `add_one` function?

  • @sreekanthpr

    @sreekanthpr

    2 жыл бұрын

    yes

  • @urbex9797

    @urbex9797

    2 жыл бұрын

    In practice, yes. But really, no, it's not necessary. The compiler can even decide to reuse the space previously used by `a`. Because at that point, `a` is no longer used.

  • @robertandrzejczyk
    @robertandrzejczyk Жыл бұрын

    It is a cool video. Not only for Rustaceans.

  • @Mytest437
    @Mytest4377 ай бұрын

    Very good

  • @zeevkotzer549
    @zeevkotzer549 Жыл бұрын

    Thanks! What tts do you use?

  • @sreekanthpr

    @sreekanthpr

    Жыл бұрын

    Microsoft one note >> View >> Immersive reader

  • @ehsankhorasani_
    @ehsankhorasani_ Жыл бұрын

    why does the kernel add some paddings in memory when storing enums. I know it's to have the same size for every enum type. but why do we have to have the same size?