Rust Modules - Explained Like I'm 5

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 มิ.ย. 2024
  • Today we are learning about modules in the Rust programming language by going through a code example!
    📝 Get your FREE Rust cheat sheet: www.letsgetrusty.com/cheatsheet
    Chapters:
    0:00 Intro
    0:45 High-Level Explanation
    1:34 Code Example
    4:33 Adding Inline Modules
    13:49 Moving Modules to Different Files
    19:29 Outro
    #letsgetrusty​​ #rust​lang​ #tutorial
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    📝 Get your FREE Rust cheat sheet : www.letsgetrusty.com/cheatsheet

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

      I got mine, tho I don't like mailing lists too much :/

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

      Loving the cheat sheet..i keep open on a tab always

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

      indeed, I would have liked to checked it out, but don't want to sign up to anything

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

      really love the cheatsheet! is there any chance a v2 is planned (simply a bit more readable / good looking / maybe dark themed?)
      Thanks for your videos! loving it

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

      Hate the signup mailing list. So skipped the cheatsheet.

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

    Honestly speaking this is probably the single most confusing aspect of Rust I faced, much more so than the whole ownership thing. I think it might be down to how little attention it was given in various Rust learning resources (Rust Book in particular). It took me several months of scratching my head and smushing my face into the screen, combined with extensive experimenting on my personal projects for me to fully understand the whole system, and you sir managed to do it under 20 minutes. God damn. Mad respect.

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

      Yeah, same here.

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

      This!!!!

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

      Same :) This video is absolutely awesome!

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

      me too!!

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

      Thanks God I was able to find the video in only a week of smashing the face into the screen...

  • @----__---
    @----__--- 2 ปีที่แล้ว +38

    I have read many blogs before getting the gist of the module system of rust. Actually the only thing I needed was this video lol

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

    It's nice that you've kept the format of videos on fundamentals similar to your coverage of the rust book, it's like having an extended version!

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

    Yeah, the modules did confuse me a lot. During this video I was thinking: "Oh, okay, now I finally get it. How great he explains it so clear." and then I remember that the video is actually called "Explained Like I'm 5". Now I feel I have to study programming as if I was five :-D

  • @shirazbabar8892
    @shirazbabar8892 11 หลายเดือนก่อน +4

    Oh, man, you saved me! I was about to give up and head back home because I was overwhelmed by the complexity of these modules. But then I stumbled upon you, and now I feel like I'm back in a playground, ready to tackle anything

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

    Excellent video! Really appreciate how you broke this down.

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

    I've googled "ho to use modules in Rust" so so many time. It is a pleasure to see such a useful, straight forward explanation on that topic.
    loop { 👍}

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

    A great video, that's cleared the a major point of confusion up for me. It's like 'use' has been overridden, like 'static' in C - for external module use, it's 'importing' and 'simplifying' external crate components, whereas for local modules it's merely 'simplifying' part of the local module. So subtly different actions, but with the same name. You've clarified that for me, so thanks!

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

    This is by far the best explanation of Rust module system I've seen (or read) so far. Very thoroughly explained. I've reentered the Rust orbit after almost two years again and had forgotten about the intricacies of the Rust module concept in the mean time. Now I feel like I am back on track again. This video is pure gold. Thanks!

  • @seyeakintunde8225
    @seyeakintunde8225 ปีที่แล้ว +5

    I was reading Chapter 7 of the Rust Book and found the example a little confusing so I searched for an alternative source of information abuot modules in Rust. This was just what I needed. Thank a lot Rusty!!!

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

    God, thank you. I was so confused about modules. Having read dozens of documents about this topic getting even more confused. Now everything makes sense.

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

    Perfect screen size / text. Great video.

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

    You're a legend. Learning Rust as my first coding language and am going to binge watch your videos to help accelerate the process. Thank you so much for taking the time to put these videos together 🙌🏾🙏🏾🔥

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

      Rust is not the easiest way to learn coding, but it will certainly help you in the long run. Good luck !

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

    Thank you. I
    think that this video is much more informative than the module episode of the rust book series and the chapter of the rust book itself!!
    I was bugged thinking about a module not "export itself" but being declared in the parent module

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

    As a JS developer I need more videos like this. Thanks

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

    You made these easy for me to understand. Now I understand how Rust's module system works. thank you.

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

    AMAZING explanation and step-by-step tutorial, thank you!

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

    Many might already have said this, but this was the achilles heel of my rust experience so far...
    Thank you very much for making this video!

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

    Excellent pace of the video. These videos are perfect as I try to learn and start using Rust!

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

    Great topic! Took something that can be difficult to understand and presented it in a simple step by step presentation! My favorite!

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

    This is the best explanation that I have found so far. Rust modules were a little bit confusing because they work differently from how organisation of code world in other languages.

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

    👍Great job! I'd like to learn more about benchmarking/profiling of Rust code. Another thing that is confusing is why we need to write anon lifetime specifier _ (underline) . I see it in very often in trait/struct decl/def.

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

    That's precisely what i needed, a simple, concise and comprehensive explanation. Now i got it, thanks a lot!

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

    Your explaination was perfect! Great job

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

    Hi! Please include this video in the let's get rusty tutorial series. It's really helpful in visualizing the module system, especially the refactoring approach you took. And exploring the 2018 pattern (came across this in the wild and I was quite confused )

  • @kumaraguru.m6205
    @kumaraguru.m6205 หลายเดือนก่อน

    Awesome , Clear understanding ❤ Thanks for this video 🎉

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

    Great video and channel in general, you answered all my questions i had.

  • @dagoberttrump9290
    @dagoberttrump9290 12 วันที่ผ่านมา

    Really nice well rounded and concise presentation, thanks!

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

    This is such a helpful channel whenever I try to learn a bit more about rust thank you!

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

    Thank you for this quick explanation, much appreciated.

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

    Amazing Video. Out of so many I've watched on this topic, this is the one that made everything click for me. Thank you

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

    The best explanation about Rust´s module system I got! This is all really confusing, but now it seems to make some sense 😊 Thank you!

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

    Really well described! I'd love if you could make a video about async/await as well as how it differs from its implementation in more popular languages like Javascript. This topic has always been tricky to wrap my head around. I think a lot of viewers would benefit from a solid explanation LGR style :)

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

    Well done. This is exactly what I needed!

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

    THANK YOU for this! Modules finally clicked for me!
    Request: a similar video (or videos) breaking down Cargo crates. For example, crates with multiple binaries and projects with nested Cargo configs.

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

    Nice video! Now I finally understand the Rust modules and project structure. As there are 2 approaches mentioned in this video to struct the modules and project files, may I know which one approach is more popular or is most used in the practice? Thanks a lot!

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

    Wathed 3 tuturials. This one clearly stands out! 👍

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

    Great video that helps a lot in understanding the concept of modules in Rust

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

    Nice and detailed explanation of a confusing subject in Rust! I'll still have to play around a bit...but this was really insightful !

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

    One of the most important and useful videos in Rust learning. Many thanks!

  • @AK-vx4dy
    @AK-vx4dy 9 หลายเดือนก่อน +1

    Very good explanation!
    Modules should be explaind again and again beacuse they are vital part of rust encapsulation approach.

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

    It would be nice if you cover cross development and debugging for the Raspberry Pi
    and it would also be nice to cover using Rust for embedded systems (no runtime), for example on Arduino.

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

    Amazing. this was really helpful and concise

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

    Perfectly explained, cheers.

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

    I bet this will be your most popular video in 5 years time. Graet job.

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

    Great tutorial 👍 Thank you so much 🙏

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

    bro, your video is so good. I have know the concept of rust modules. Thanks very much.

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

    cool - now I am able to structure my code into models. Thank you.
    It would be fine to learn something about testing in Rust.

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

    Very very very useful!!! Next could you please cover conditional compilation, build flags, build.rs, etc.? Thanks a lot

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

    This is such impressive teaching. Good job.

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

    Thank you so much for this great content, this is very valuable for me!

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

    Okay, convoluted conventions but once understood they make sense and feel clean and powerful enough to maintain code properly factored and under control.
    Thanks a lot for making this video, it was a day saver!

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

    great explanation, thanks a lot!

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

    HOLY COW , this is TEH hands down best explenation on the net for Rust Newcommers! This Explains Use vs Mod perfect! Thank you so much!

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

    thanks man, this is really great video.

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

    thank you, very well explained!

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

    clear and concise explanation

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

    Thank you so much for this!

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

    Rust Made Easy with Let's Get Rusty! you made it really easy to understand!

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

    I'm a bit confused with re export. In a nutshell does it mean making nested modules functions, structs etc... within your project src public for use at the top level, so anyone can use the function? Like a public interface?

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

    Amazing, thank you very much!

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

    This video is pure gold!

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

    Great stuff, thank you sharing.

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

    I am getting into programming and learning Rust, I think it's pleasantly elegant!

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

    Now that I think of it, is there a VSC extension/configuration for sorting and files in the tree regardless of their type? In order to group the modules with their sub-modules..

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

    In my project, when I added more folder branches within 'src', the local code worked normally (build, fmt, test). But when I executed the same in the CI/CD process, it was as if I couldn't find the imports in that format...

  • @PhosphorusMoscu-code
    @PhosphorusMoscu-code 2 ปีที่แล้ว

    You explications are very good! Thanks for aclarate this topic.

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

    Kindly cover some (design) patterns in how to export rust functions to swift and kotlin via FFI. Thank you, your video on rust modules is very helpful.

  • @Only-zeus
    @Only-zeus 2 ปีที่แล้ว

    Happy new year bro cheers!! 🍻

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

    This concept is really easy to understand on small projects, but I would still love to see a bigger project example

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

    Nice job. In IntelliJ I find it convenient to just create modules in line and then tell IntelliJ to turn them into separate files.

  • @TheBest-sd2qf
    @TheBest-sd2qf ปีที่แล้ว

    Something I often wonder about, does it speed up compile time if you split up your project in multiple modules?

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

    Would one recommend this same structure used for libraries in applications as well?

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

    awesome video. this is what I want to know. there are some minor differences then javascript but rust's module system is more effective and sense I think.

  • @M-Daeva
    @M-Daeva ปีที่แล้ว

    2:31 How to setup VSCode to move automatically text cursor with indentation after pressing enter inside curve brackets? Is it rust-analyzer settings option?

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

    What's up rusty! everything fine? Came here to complete one more monster video about Rust! Thank you for sharing your knownledges with us.
    RUST ❤

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

    It was Awesome bro!

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

    best rust channel out there

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

    It seems in 2023, you need to run " cargo modules --with-types --with-fns " to get the same output as shown in the video.
    Went on a detour trying to see why my output differed and that was the result.

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

    Thanks for this awesome video. Net video that I would request you to cover is `error handling` may be using thiserror and anyhow.

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

    If you try this today, you need to install cargo-modules using this command: cargo install cargo-modules --locked. It won't work any other way. It needs to install a million things and some of those things don't play nice together dependency wise.
    Also, the command to use to generate the try with types is now: cargo modules generate tree --types, The author(s) of cargo-modules have seemingly introduced several feature breaking changes on top of creating the dependancy nightmare that makes dll hell pale in comparison.

    • @kqvanity
      @kqvanity 22 วันที่ผ่านมา

      It did install for me without appending the --locked flag, but the subcommands are totally different

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

    It would be awesome if you did some videos on async/await

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

    So the only things crate consumers can use from a crate are items in the crate module marked pub? The items marked pub in sub-modules are only pub for the crate module, and need to be explicitly "re-pub'd" from the crate module to also be made available to the crate consumer?

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

    Okay but let's say we have another sub directory next to auth_utils, and we want to make use of auth_utils from inside that directory. In Iib.rs we could just use mod auth_utils to make use of its sub modules and functions. How would this work inside a file in a neighboring subdirectory?

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

    Finally ! Bogdan thank you for this. Rust Yedi !

    • @ari-mcbrown
      @ari-mcbrown 2 ปีที่แล้ว

      omg, I couldn't put my finger on it... You're right. He's the chosen one. ;)

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

    Really well explained. (Even I understood it.)

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

    I'm still confused about the pub keyword.
    Wouldn't the Status struct also be public outside of the crate? As you gave it the pub keyword? Or only the stuff on the "lower" module get "exported"?.

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

      "If an item is public, then it can be accessed externally from some module m if you can access all the item's ancestor modules from m." - The Rust Reference
      The database module is private so you can't access it's items outside the auth_service library crate.

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

      @@letsgetrusty Oh, I did not read that on the The Book.
      Thanks!

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

    I kinda stuck on a problem where I can't make traits use the pub keyword when they are inside a module (so kinda want to use inheritance as to what i have learned in php). Like the animal human example in rust where traits are implemented. But seems like my approach is kinda wrong. I happen to watch the video about builder pattern and it seems pretty much "fixed" my problem. It pretty much avoid you of having creating a struct that only one value is different but almost have the same methods.

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

    the "if let Status::Connected" line prevents me from compiling your code, "error[E0658]: `let...else` statements are unstable"

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

    Man I wished this video existed before I struggled to make sense of modules.

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

    This was much easier to understand than the official book.

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

      Seeing someone do it while talking through it is a big help.

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

    Amazing!

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

    Awesome.

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

    Is the filename you should use always mod.rs?
    So if I had three different submodules that contain even more modules I had three directories with their own mod.rs file?

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

      Yes your understanding is correct.

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

      @@letsgetrusty Thank you very much :)

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

    hey, I have installed cargo modules. but when I run the command as like your command. its not showing me similar result to your. it just show mod and struct and enum.
    what the problem here.

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

    Are there changes in module system with 2021 edition ?

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

    Good stuff

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

    Your videos are Rust gold :P

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

    Great video. But I am still confused. I want the rust equivalent to the following code from C++.
    // file: main.c
    void ace();
    int main() { ace(); return 0;}
    // file: a.c
    void bar();
    void ace() { bar();}
    // file: b.c
    void bar() { std::cout