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

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 มิ.ย. 2024
  • 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

ความคิดเห็น • 178

  • @DenisFalqueto
    @DenisFalqueto 2 ปีที่แล้ว +111

    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 ปีที่แล้ว +4

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

    • @dreastonbikrain1896
      @dreastonbikrain1896 ปีที่แล้ว +1

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

  • @snwbrdr36
    @snwbrdr36 2 ปีที่แล้ว +68

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

  • @aqua3418
    @aqua3418 2 ปีที่แล้ว +39

    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 ปีที่แล้ว +16

    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!

  • @Onkoe
    @Onkoe ปีที่แล้ว +1

    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! :)

  • @sreekanthpr
    @sreekanthpr  2 ปีที่แล้ว +67

    *UPDATE: This video is re-uploaded with a better audio here* th-cam.com/video/7_o-YRxf_cc/w-d-xo.html
    Corrections:
    * 6:10, it should be 2^64 - 1 (without parenthesis)
    * Trait is dynamically sized. Trait object has known size.

    • @dersebastianbauer
      @dersebastianbauer 2 ปีที่แล้ว +4

      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 ปีที่แล้ว +1

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

    • @parry84
      @parry84 2 ปีที่แล้ว +3

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

    • @noop9770
      @noop9770 ปีที่แล้ว +2

      * 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
    @joelimbergamo639 2 ปีที่แล้ว +3

    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 ปีที่แล้ว +2

    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 ปีที่แล้ว +3

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

  • @GeorgeFosberry
    @GeorgeFosberry ปีที่แล้ว +1

    A nice overview and/or refresher.

  • @juanma3333
    @juanma3333 2 ปีที่แล้ว +2

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

  • @ClearerThanMud
    @ClearerThanMud 2 ปีที่แล้ว +3

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

  • @norberthajagos
    @norberthajagos 11 หลายเดือนก่อน

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

  • @adammontgomery7980
    @adammontgomery7980 2 ปีที่แล้ว +1

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

  • @bugsenthil
    @bugsenthil 5 หลายเดือนก่อน

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

  • @procyonq
    @procyonq ปีที่แล้ว +1

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

  • @rajesh_404
    @rajesh_404 ปีที่แล้ว +1

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

  • @user-rs4sg2tz6k
    @user-rs4sg2tz6k ปีที่แล้ว +1

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

  • @MasterSpencer2483
    @MasterSpencer2483 3 หลายเดือนก่อน

    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

  • @mohitpawar8582
    @mohitpawar8582 2 ปีที่แล้ว +1

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

  • @jeffvandyke1239
    @jeffvandyke1239 2 ปีที่แล้ว +1

    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.

  • @tuanlam999
    @tuanlam999 ปีที่แล้ว

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

  • @rajesh_404
    @rajesh_404 ปีที่แล้ว +1

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

  • @360nickx
    @360nickx 2 ปีที่แล้ว +12

    Thanks for this. It is brilliant!

  • @gneryze
    @gneryze 2 หลายเดือนก่อน +1

    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

  • @sokolorlov996
    @sokolorlov996 9 หลายเดือนก่อน

    Visualization helps a lot, thanks!

  • @ikhlasulkamal5245
    @ikhlasulkamal5245 ปีที่แล้ว

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

  • @mokorra
    @mokorra 10 หลายเดือนก่อน

    very informative! thank you

  • @JoaoBosco89
    @JoaoBosco89 ปีที่แล้ว

    Your video helped me a lot. Thank you.

  • @xyve6129
    @xyve6129 8 หลายเดือนก่อน +2

    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.

  • @marcoantonio7648
    @marcoantonio7648 ปีที่แล้ว

    This video is gold. Thanks for the hard work!

  • @jonathanmoore5619
    @jonathanmoore5619 2 ปีที่แล้ว +2

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

  • @user-ow5mn6dn7n
    @user-ow5mn6dn7n 2 ปีที่แล้ว

    Dude, amazing work! Just amazing

  • @user-io4sr7vg1v
    @user-io4sr7vg1v 2 หลายเดือนก่อน

    Wonderful. So thorough.

  • @leonlee9518
    @leonlee9518 9 หลายเดือนก่อน

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

  • @ancientITguy
    @ancientITguy 2 ปีที่แล้ว

    Great explanation! Thanks

  • @m50505
    @m50505 ปีที่แล้ว

    thank you for sharing knowledge. appreciate the effort.

  • @shreyashsaitwal4244
    @shreyashsaitwal4244 ปีที่แล้ว +1

    This is gold! Thank you.

  • @mustafaerdemkoc2529
    @mustafaerdemkoc2529 2 ปีที่แล้ว

    Thank you! this was very helpful.

  • @abteyeb7969
    @abteyeb7969 2 ปีที่แล้ว

    very good and clear

  • @malharvora1281
    @malharvora1281 2 ปีที่แล้ว +1

    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 TH-cam but only a few like this, are actually understandable by novice Rustaceans.

  • @askhatarslanov6327
    @askhatarslanov6327 ปีที่แล้ว +1

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

  • @Leonardo-ms6re
    @Leonardo-ms6re 2 ปีที่แล้ว +2

    This is gold! Congrats :)

  • @young_kido2293
    @young_kido2293 ปีที่แล้ว

    Thank you man for sharing this stuff

  • @soapbubblesx
    @soapbubblesx 2 ปีที่แล้ว

    Enlightening! Thank you!

  • @vmscode
    @vmscode ปีที่แล้ว +1

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

  • @Nre11
    @Nre11 2 ปีที่แล้ว

    Wow, honestly incredible

  • @sanjusk4287
    @sanjusk4287 2 ปีที่แล้ว +1

    Such wisdom, oh great one.

  • @InMemoryOfNeo
    @InMemoryOfNeo 9 หลายเดือนก่อน

    awesome explanation 👏👏👏👏👏

  • @xenocampanoli815
    @xenocampanoli815 7 หลายเดือนก่อน

    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.

  • @reneribaud8912
    @reneribaud8912 3 หลายเดือนก่อน

    Amazing video thanks.

  • @arash2229
    @arash2229 ปีที่แล้ว

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

  • @b5eu6b87
    @b5eu6b87 2 ปีที่แล้ว

    extremely well done video!

  • @georgereasonproductions
    @georgereasonproductions 2 ปีที่แล้ว +1

    love to learn thanks for sharing

  • @ozanmuyes
    @ozanmuyes 2 ปีที่แล้ว

    This is golden, thanks!

  • @RAHUDAS
    @RAHUDAS 2 ปีที่แล้ว

    great work, very helpful for me

  • @dk5146
    @dk5146 ปีที่แล้ว

    need more videos like this

  • @isaaccespedes4817
    @isaaccespedes4817 10 หลายเดือนก่อน

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

  • @akatsukinoh7660
    @akatsukinoh7660 ปีที่แล้ว

    This is exactly i want. Thank you!

  • @scorpo999
    @scorpo999 ปีที่แล้ว

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

  • @tanuvishu
    @tanuvishu 2 ปีที่แล้ว

    Awesome explaination

  • @tadeohepperle
    @tadeohepperle ปีที่แล้ว

    This is so useful! Thanks

  • @MrSasukeUchihaEMS
    @MrSasukeUchihaEMS 2 ปีที่แล้ว

    good stuff, thank you very much

  • @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

  • @chudchadanstud
    @chudchadanstud 8 หลายเดือนก่อน

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

  • @morgomi
    @morgomi ปีที่แล้ว

    the best introduction has ever seen.

  • @brethagen7776
    @brethagen7776 6 หลายเดือนก่อน

    This video is beautiful

  • @urbex9797
    @urbex9797 2 ปีที่แล้ว

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

  • @jokerdevilarm
    @jokerdevilarm 2 ปีที่แล้ว +1

    The best video!!! Thnx

  • @HansFlamme
    @HansFlamme 2 ปีที่แล้ว

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

  • @parthivmangukiya5953
    @parthivmangukiya5953 2 ปีที่แล้ว

    Thanks!!! Your video is too good

  • @ehkhe2650
    @ehkhe2650 ปีที่แล้ว +1

    18:50 Couldn't the compiler get the size for a slice from it's initializer? It should know the size is 2 if I say 0..2. It's something like `constexpr` in C++.

  • @xl0xl0xl0
    @xl0xl0xl0 ปีที่แล้ว

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

  • @dionysis_
    @dionysis_ 2 ปีที่แล้ว

    That was very useful 👍

  • @snk-js
    @snk-js 2 ปีที่แล้ว

    this is the best video I have ever watched on youtube

  • @nurmohammed9642
    @nurmohammed9642 2 ปีที่แล้ว +2

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

  • @maximus1172
    @maximus1172 ปีที่แล้ว

    I cannot thank you enough 🙏

  • @michaelmueller9635
    @michaelmueller9635 ปีที่แล้ว

    This is high quality stuff!

  • @Policritics
    @Policritics 2 ปีที่แล้ว

    This is the best explain to Rust

  • @ingmarfalk3306
    @ingmarfalk3306 6 หลายเดือนก่อน

    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

  • @RivoMPutra
    @RivoMPutra ปีที่แล้ว

    just beca a subscriber keep up the good work Sir.

  • @happilyconfuseddog8951
    @happilyconfuseddog8951 ปีที่แล้ว

    Great ty ty

  • @user-xg9wt3he9q
    @user-xg9wt3he9q 2 ปีที่แล้ว

    Super! Thanks

  • @user-vi2jw2rc9u
    @user-vi2jw2rc9u 2 ปีที่แล้ว

    thank you for this

  • @nav-on
    @nav-on ปีที่แล้ว

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

  • @jeffg4686
    @jeffg4686 ปีที่แล้ว

    @15:47 Anyone know if the "padding" memory used up will have an effect when loaded into a cpu cache. Does it get condensed when loaded to cpu, or is the padding still there (taking up extra cpu cache)?

  • @alexloktionoff6833
    @alexloktionoff6833 6 หลายเดือนก่อน

    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?

  • @aaryanpashine6633
    @aaryanpashine6633 10 หลายเดือนก่อน

    holy shit this was amazing

  • @JasonFritcher
    @JasonFritcher 2 ปีที่แล้ว +4

    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 ปีที่แล้ว +1

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

  • @titogorla
    @titogorla ปีที่แล้ว

    thanks

  • @uncleyour3994
    @uncleyour3994 ปีที่แล้ว

    awesome, cool

  • @10e999
    @10e999 2 ปีที่แล้ว

    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 ปีที่แล้ว +1

      yes

    • @urbex9797
      @urbex9797 2 ปีที่แล้ว +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.

  • @whtiequillBj
    @whtiequillBj 9 หลายเดือนก่อน

    19:54, I wish that proper memory visualizations were given for string and str as they are not represented in memory the same way.

  • @luiscarlosjayk
    @luiscarlosjayk 2 หลายเดือนก่อน

    Why the Array(Vec) variant of the Data enum is stored in stack if in the Vector section of the video it was explained that Vectors were stored in the heap so they can grow.

    • @sreekanthpr
      @sreekanthpr  หลายเดือนก่อน +1

      The three elements that constitute the Vec header (pointer to data, length and capacity) are stored on the stack. The Vec elements will be stored in the heap.

    • @luiscarlosjayk
      @luiscarlosjayk หลายเดือนก่อน

      @@sreekanthpr thank you!!!!, I watched that section again and now I understood it better. I loved this visual explanation, everyone else does it with charts and words, but having it visually explained like you did totally improves and easies the understanding of these abstractions. Again, thank you!

  • @deepmehta3840
    @deepmehta3840 2 ปีที่แล้ว

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

  • @lavienrozy1867
    @lavienrozy1867 2 ปีที่แล้ว

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

  • @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?

  • @hariangr
    @hariangr 2 ปีที่แล้ว

    so freakin good

  • @tvboyd23
    @tvboyd23 ปีที่แล้ว

    possibly the single most based rust video on yt

  • @robertandrzejczyk
    @robertandrzejczyk ปีที่แล้ว

    It is a cool video. Not only for Rustaceans.