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

Поделиться
HTML-код
  • Опубликовано: 6 июн 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!

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

  • @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)]`

  • @sreekanthpr
    @sreekanthpr  2 года назад +67

    *UPDATE: This video is re-uploaded with a better audio here* ruclips.net/video/7_o-YRxf_cc/видео.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

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

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

  • @juanma3333
    @juanma3333 2 года назад +2

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

  • @sanket1729
    @sanket1729 Год назад +3

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

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

  • @norberthajagos
    @norberthajagos 11 месяцев назад

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

  • @procyonq
    @procyonq Год назад +1

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

  • @rajesh_404
    @rajesh_404 Год назад +1

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

  • @bugsenthil
    @bugsenthil 5 месяцев назад

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

  • @GeorgeFosberry
    @GeorgeFosberry Год назад +1

    A nice overview and/or refresher.

  • @360nickx
    @360nickx 2 года назад +12

    Thanks for this. It is brilliant!

  • @adammontgomery7980
    @adammontgomery7980 2 года назад +1

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

  • @ClearerThanMud
    @ClearerThanMud 2 года назад +3

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

  • @marcoantonio7648
    @marcoantonio7648 Год назад

    This video is gold. Thanks for the hard work!

  • @MasterSpencer2483
    @MasterSpencer2483 4 месяца назад

    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

  • @user-ow5mn6dn7n
    @user-ow5mn6dn7n 2 года назад

    Dude, amazing work! Just amazing

  • @Leonardo-ms6re
    @Leonardo-ms6re 2 года назад +2

    This is gold! Congrats :)

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

  • @jonathanmoore5619
    @jonathanmoore5619 2 года назад +2

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

  • @shreyashsaitwal4244
    @shreyashsaitwal4244 Год назад +1

    This is gold! Thank you.

  • @mohitpawar8582
    @mohitpawar8582 2 года назад +1

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

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

  • @askhatarslanov6327
    @askhatarslanov6327 Год назад +1

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

  • @ikhlasulkamal5245
    @ikhlasulkamal5245 Год назад

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

  • @Nre11
    @Nre11 2 года назад

    Wow, honestly incredible

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

  • @b5eu6b87
    @b5eu6b87 2 года назад

    extremely well done video!

  • @ozanmuyes
    @ozanmuyes 2 года назад

    This is golden, thanks!

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

  • @mustafaerdemkoc2529
    @mustafaerdemkoc2529 2 года назад

    Thank you! this was very helpful.

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

  • @soapbubblesx
    @soapbubblesx 2 года назад

    Enlightening! Thank you!

  • @ancientITguy
    @ancientITguy 2 года назад

    Great explanation! Thanks

  • @sanjusk4287
    @sanjusk4287 2 года назад +1

    Such wisdom, oh great one.

  • @young_kido2293
    @young_kido2293 Год назад

    Thank you man for sharing this stuff

  • @user-io4sr7vg1v
    @user-io4sr7vg1v 2 месяца назад

    Wonderful. So thorough.

  • @chudchadanstud
    @chudchadanstud 8 месяцев назад

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

  • @arash2229
    @arash2229 Год назад

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

  • @snk-js
    @snk-js 2 года назад

    this is the best video I have ever watched on youtube

  • @m50505
    @m50505 Год назад

    thank you for sharing knowledge. appreciate the effort.

  • @michaelmueller9635
    @michaelmueller9635 Год назад

    This is high quality stuff!

  • @JoaoBosco89
    @JoaoBosco89 Год назад

    Your video helped me a lot. Thank you.

  • @tadeohepperle
    @tadeohepperle Год назад

    This is so useful! Thanks

  • @sokolorlov996
    @sokolorlov996 9 месяцев назад

    Visualization helps a lot, thanks!

  • @leonlee9518
    @leonlee9518 9 месяцев назад

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

  • @brethagen7776
    @brethagen7776 6 месяцев назад

    This video is beautiful

  • @akatsukinoh7660
    @akatsukinoh7660 Год назад

    This is exactly i want. Thank you!

  • @isaaccespedes4817
    @isaaccespedes4817 10 месяцев назад

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

  • @vmscode
    @vmscode Год назад +1

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

  • @mokorra
    @mokorra 10 месяцев назад

    very informative! thank you

  • @morgomi
    @morgomi Год назад

    the best introduction has ever seen.

  • @RAHUDAS
    @RAHUDAS 2 года назад

    great work, very helpful for me

  • @dionysis_
    @dionysis_ 2 года назад

    That was very useful 👍

  • @parthivmangukiya5953
    @parthivmangukiya5953 2 года назад

    Thanks!!! Your video is too good

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

  • @HansFlamme
    @HansFlamme 2 года назад

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

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

  • @reneribaud8912
    @reneribaud8912 3 месяца назад

    Amazing video thanks.

  • @tanuvishu
    @tanuvishu 2 года назад

    Awesome explaination

  • @nurmohammed9642
    @nurmohammed9642 2 года назад +2

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

  • @jokerdevilarm
    @jokerdevilarm 2 года назад +1

    The best video!!! Thnx

  • @abteyeb7969
    @abteyeb7969 2 года назад

    very good and clear

  • @scorpo999
    @scorpo999 Год назад

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

  • @georgereasonproductions
    @georgereasonproductions 2 года назад +1

    love to learn thanks for sharing

  • @dk5146
    @dk5146 Год назад

    need more videos like this

  • @xl0xl0xl0
    @xl0xl0xl0 Год назад

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

  • @MrSasukeUchihaEMS
    @MrSasukeUchihaEMS 2 года назад

    good stuff, thank you very much

  • @maximus1172
    @maximus1172 Год назад

    I cannot thank you enough 🙏

  • @InMemoryOfNeo
    @InMemoryOfNeo 10 месяцев назад

    awesome explanation 👏👏👏👏👏

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

  • @user-vi2jw2rc9u
    @user-vi2jw2rc9u 2 года назад

    thank you for this

  • @user-xg9wt3he9q
    @user-xg9wt3he9q 2 года назад

    Super! Thanks

  • @nav-on
    @nav-on Год назад

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

  • @Policritics
    @Policritics 2 года назад

    This is the best explain to Rust

  • @urbex9797
    @urbex9797 2 года назад

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

  • @aaryanpashine6633
    @aaryanpashine6633 10 месяцев назад

    holy shit this was amazing

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

  • @RivoMPutra
    @RivoMPutra Год назад

    just beca a subscriber keep up the good work Sir.

  • @deepmehta3840
    @deepmehta3840 2 года назад

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

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

  • @hariangr
    @hariangr 2 года назад

    so freakin good

  • @uncleyour3994
    @uncleyour3994 Год назад

    awesome, cool

  • @happilyconfuseddog8951
    @happilyconfuseddog8951 Год назад

    Great ty ty

  • @nbes6328
    @nbes6328 2 года назад

    Awesome

  • @wuoyun6302
    @wuoyun6302 2 года назад

    thanks

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

  • @Mytest437
    @Mytest437 6 месяцев назад

    Very good

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

  • @chalmagean
    @chalmagean 2 года назад

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

    • @sreekanthpr
      @sreekanthpr  2 года назад +1

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

  • @lavienrozy1867
    @lavienrozy1867 2 года назад

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

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

  • @talalkalai8748
    @talalkalai8748 Год назад

    can u upload same kind of video for cpp r/l values / move semantics?

  • @zeevkotzer549
    @zeevkotzer549 Год назад

    Thanks!
    What tts do you use?

    • @sreekanthpr
      @sreekanthpr  Год назад +1

      Microsoft one note >> View >> Immersive reader

  • @drsensor
    @drsensor Год назад

    Could you put it in the video description of what NTTS model/engine you are using to dub this amazing video?

    • @sreekanthpr
      @sreekanthpr  Год назад

      I use Microsoft onenote/word >> View >> Immersive Reader