Rust's Most Important Containers 📦 10 Useful Patterns

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ต.ค. 2022
  • A walkthrough and explanation of 10 useful patterns involving Rust's Option and Result containers.
    -
    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
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @gdnight
    @gdnight ปีที่แล้ว +235

    The complexity progression through the video is super helpful, great format, you should do more like this one.

    • @codetothemoon
      @codetothemoon  ปีที่แล้ว +11

      Thanks, very happy the format worked for you! Will definitely create more.

  • @swanandx
    @swanandx ปีที่แล้ว +152

    Clicked on video blazingly fast

    • @TON-vz3pe
      @TON-vz3pe ปีที่แล้ว +5

      Me too

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

      Same

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

      Yeah... Same....

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

    Honestly, i couldn't get my head wrap around on these error handlings before but not anymore . Thank you so much!!!

  • @ZantierTasa
    @ZantierTasa ปีที่แล้ว +16

    It's sooo useful in Rust to read through and practice the Option, Result, and slice functions. They show up everywhere, and the standard library provides loads of useful functions.

  • @AreQ212
    @AreQ212 ปีที่แล้ว +27

    Great video, as always :D One think which is also worth mentioning is that, you can implement `From` trait on your custom error type and use question mark operator.
    Example:
    ```
    #[derive(Debug)]
    struct SummationError;
    impl From for SummationError {
    fn from(_: ParseIntError) -> Self {
    SummationError
    }
    }
    [...]
    fn sum_str_vec(strs: &[String]) -> Result {
    let mut acc = 0;
    for s in strs {
    acc += to_int(s)?;
    }
    Ok(acc.to_string())
    }
    ```

  • @aussieexpat
    @aussieexpat 10 วันที่ผ่านมา

    This really helped me a lot. the functions and containers are so similar it's nice to have a run through these patterns.

  • @thisisscotts
    @thisisscotts ปีที่แล้ว +56

    Great video. I've only started learning Rust recently and just the other night a light bulb went on in my head as to the whole point of Result and unwrap. This video was therefore great timing for me.

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

      Nice thisisscotts! Really happy you found it valuable!

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

      Although I have been fiddling with programming for decades in my teens, when rust came out I was so bothered, like why all these unwraps and whatnot?
      Then I took computer science, and one of the modules taught about these optionables and nullables (yes, java) and also a custom "Result" wrapper we had to make. My professor hated nulls. It was hilarious because my other mod would use and abuse nulls.
      It was actually to teach the concepts of monads.
      Now when i watch this video, I laugh at myself because this video makes it so clear and easy what these things are.

  • @SomethingSomething1337
    @SomethingSomething1337 ปีที่แล้ว +15

    This video was very insightful! Thank you for sharing, as someone new to rust, seeing all the Option and Result helper methods demonstrated with such simplicity is very helpful.
    I would have wrote match patterns all over the place instead because I didn’t know about these functions, thanks again !

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

      Great Jaime, I'm really happy you found it valuable! Thanks for watching!

  • @TheWombatGuru
    @TheWombatGuru ปีที่แล้ว +12

    Amazing exposition of the ways to handle simple errors like this one. I myself thought the introduction of the problem at 1:46 was a little too fast, but that became clear in immediately afterwards.

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

      Thanks WombatGuru, and thanks for the feedback! I did blow through the explanation of the the program really quickly, I'll break it down a bit more next time!

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

    One of the best videos I've seen about these for someone like me that's just learning Rust

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

    That was excellent details on Result and Option. Thank you!

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

      thanks really happy you found it valuable!

  • @bassam.2023
    @bassam.2023 หลายเดือนก่อน +1

    As a rust beginner, I've now watched this video about 6 times over the course of the last week. It's an excellent reference! Thanks! 🙏🏼

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

      love this!! glad it's been valuable!

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

    My first exposure to Results and Options were at my internship where I was writing C# based on the railway-oriented programming paradigm. My goodness, I love not dealing or worrying about nulls, and Rust handles this form of error handling so beautifully.

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

    BEST VIDEO !!! THIS WAS THE ONLY THING THAT KEPT ME FROM UNDERSTANDIGN RUST !

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

      glad you found it valuable!

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

    These containers are so fundamental to the language.

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

    Loved the video, it was extremely insightful and easy to digest for a beginner like me. I hope you make more of these!

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

    Wow. Great video! It gave me new tools to handle errors aside from 'match' and 'if let' It certainly will improve my future code readability. Thank you!

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

      Thanks, glad you found it valuable!

  • @MrKeebs
    @MrKeebs ปีที่แล้ว +33

    No matter how much you think you already know, you always learn from those kinds of videos. I didn't know about map_err and, guess what, ended up using it today 🙂
    Keep up with the amazing work!

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

      Nice, glad you got something useful out of it! Thanks for the kind words!

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

      I needed map_err yesterday and this popped up on suggestions today. Guess I'll fix that unwrap today!

  • @NeatMemesDotCom
    @NeatMemesDotCom 7 หลายเดือนก่อน +2

    Each video I watch from this channel makes me less angry with Rust. 🤝

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

      nice! mission accomplished 😎

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

    Thanks for this very useful progression of code examples. As a complete beginner found it super useful to watch the video a few times before I could internalize the utility of Result and Option and the .ok , .unwrap and .err families of functions

  • @starlordcodes
    @starlordcodes 10 หลายเดือนก่อน +2

    Great Video. I wish there were more people making videos about Rust.

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

      thanks! and me too.

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

    So helpful - I've seen all these variants in code (I'm a beginner) but couldn't keep them straight in my head. Seeing you build them up from ground level straightened it out for me. Thank you!

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

    Great explanation! Thanks a lot!!!

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

    My third day into Rust and watching your video and crust of rust’s video, I am very excited about Rust. Thank you and keep making more patterns related videos. It’s very helpful specially for intermediate/experienced developers to get started quickly.

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

    Great video! I was writing a toy program in rust to learn and went through almost the same evolution. It was actually frustrating because I didn't know if I should be returning result/option or using 'if let' vs match vs unwrap_or. I was assuming there is a correct (idiomatic) way, but couldn't ever figure out what that was.

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

    Thanks for putting this out. This was very insightful for newbies like me. 🙏

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

    Just brilliant. Thank you so much.

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

    Love your videos, but one small point of improvement; use #[test] functions instead of using main for testing. Test functions are really useful for just trying out stuff, and the examples they provide dont need to be deleted just to make room for "better" code

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

      Thanks for the feedback! Yeah this would have made sense - I've been relentless about eliminating as much prerequisite knowledge as possible, maybe to a fault. You could certainly argue that writing tests is reasonable prerequisite knowledge.

  • @cheebadigga4092
    @cheebadigga4092 5 หลายเดือนก่อน +2

    Thanks, the question mark was extremely helpful to replace some of my now redundant match clauses.

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

      nice, really happy you got something out of it!

  • @chris.davidoff
    @chris.davidoff ปีที่แล้ว +1

    This is super helpful, I didn't know about that last one you showed. Thank you!!

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

      Glad you found it valuable, thanks for watching Chris!

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

    Excellent video! Would love to see more pattern videos like this to add to the toolbox

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

      Thanks JammyJ, more are on the way (I'm a poet and I didn't know it)!

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

    Great video! Learnt few new things, please keep making them

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

      Thanks gorudonu, more to come!

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

    awesome. Thank you man!

  • @jacques-dev
    @jacques-dev ปีที่แล้ว +2

    I've definitely been overusing Option when I should be using Result, thanks for your examples.

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

      nice, really happy the video helped!

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

    Really helpful video to understand t OK(), ? and map() functions. Please keep making such videos.

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

    Thank me so much for helping me through handling errors in Rust. You deepened my knowledge 🙌

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

    Insanely good and concise explanation. Thanks. You definitely know how to explain things.

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

      Thanks, really happy you found it valuable!

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

    Really great video about the subject. Thanks a lot!!!

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

      thanks glad you got something out of it!

  • @Drama-ck2tp
    @Drama-ck2tp ปีที่แล้ว +3

    Valuable information! Look forward to your videos always! As I’m currently learning rust

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

      Thanks drama, glad you found it valuable!

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

    Exactly what I needed, thanks!

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

      great, glad you found it valuable!

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

    This is a great video! The example and the explanation done extremely well! I like how you explain hard things.

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

    omg this was so useful! Thank you for good videos :)

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

    I’m new to Rust. Awesome video, super useful. Thank you!

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

      Great, glad you got something out of it! 😎

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

    I wish I had this video when I was first starting out with Rust. Great video!

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

    this cleared things up thanks

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

    Wow, that was a useful video compressed in a few minutes. Thanks!

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

      thanks, really happy it was helpful!

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

    Great video. I like how it is structured

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

      Thanks Hupa1a, cool to see that folks enjoy the format. I really having one core piece of code, then changing it slightly many times to demonstrate different concepts

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

    It's so nice !!!! pretty much thanks to your share!!!

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

      Thanks for the kind words and thanks for watching!

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

    been using similar concepts for year in Typescript via fpts. Now am getting into rust and am so glad I did because it's immediately clear to me what all this stuff is.

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

    This is a such a good informative video, i wish i found it sooner!

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

    Thanks. This practice examples are great!

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

      Thanks preeby, glad you found it valuable!

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

    Very helpful, thanks!

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

      thanks, glad you got something out of it!

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

    Excellent video! Great tempo.

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

      Thanks Eric, glad you found it valuable!

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

    Thanks again for another great video. It has definitely improved my confidence in knowing how to read and write Rust code. Looking forward to more awesome content.

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

    Very insightful content! Thank you for this.

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

    super helpful, thanks a lot

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

      glad you found it valuable, thanks for watching!

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

    Awesome thanks great video.

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

    Great video. Bookmarked it. Will likely go back to it many time in the future ;) Thanks

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

      Thanks Miriam, I hope it provides evergreen value for you!

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

    Brilliantly structured, getting me excited to get started with Rust!

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

      thank you, very happy it fueled your fire to learn Rust!

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

    Thanks, great explanation :)

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

      thanks for watching, glad you found it valuable!

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

    Nice explanation for monads from functional languages. Keep up! Thanks!

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

      Thanks for watching Artem! I managed to do it without using the word "monad" - not sure if that's a good or a bad thing 🙃

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

    excelent video thanks you!!

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

    This was very interesting and very clearly explained. Thank you

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

      thanks, glad you found it valuable!

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

    Great density of information in this video

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

    That keyboard sound on 2x, music to my ears!

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

      thanks! it sounds like many folks like the sound of the keyboard, so I actually put a mic up to it in the "Rust vs 7 Languages" video. Might try to do that more in the future.

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

    Was just reading about that in the rust book two days ago. Great timing!

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

    Very helpful!

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

      Glad you found it valuable Glenn!

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

    wow! awesome! thanks

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

    Great video! Really good and understandable structure.

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

      Thanks mivoe99, glad you found it valuable!

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

    This was an awesome video. Please do more

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

    Really great tutorials, keep them up!

  • @DavidGarcia-xz1ui
    @DavidGarcia-xz1ui 8 หลายเดือนก่อน +1

    Thanks!

  • @jeanbarbosa9
    @jeanbarbosa9 28 วันที่ผ่านมา

    Pretty good explanation, I subscribed because of this video.

  • @303pix
    @303pix 8 หลายเดือนก่อน +1

    very well explained!

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

      thank you, glad you got something out of it!

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

    Very educational!

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

    Very useful instructions

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

    That’s really well explained. I’m sharing it to a friend that want to learn Rust.

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

      Thanks Robin, and thanks for sharing it!

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

    thanks for this useful tutorial

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

      glad you found it valuable, thanks for watching!

  • @oglothenerd
    @oglothenerd 6 หลายเดือนก่อน +2

    In that use case, you can use .or(SummationError) instead of .map_err(|_| SummationError) method.

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

    The best! I need more of this please! BTW, it would be more handy if you mention thiserror and anyhow in the end. 👍👍👍

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

      Thanks Todsaporn, I'll do more like this. I also plan to do some kind of "Crate Hall of Fame" series where we tour some of the more ubiquitous crates like the ones you mentioned

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

    Great tutorial! Subscribed :)

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

    Fantastic !!!

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

    It would be nice if those 10 patterns got indexed or simply numbered in the video :)

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

      Thanks for the feedback, I may add numbers / titles in the future!

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

    rly rly great I loving all rust lectures form awesome people here in yt

  • @Felipe-53
    @Felipe-53 ปีที่แล้ว +1

    Maaaan, really throughout. WHat a nice video, wow. I'm impressed. THanks

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

      thank you, glad you got something out of it!

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

    super helpful

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

    Incredible content, keep up!

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

      thanks so much, glad you found it valuable!

  • @Felipe-53
    @Felipe-53 ปีที่แล้ว +1

    You are awesome. Thanks for the excellent content!

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

    This video was incredibly well made, it is hard to match. Thank you for the great content.

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

      thanks so much for the kind words!

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

    Man, you've addressed my inner angry funcy npc at 2:28 🤣

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

    this is great. would also be great to follow up with a part to on From and when it's appropriate. i've learned enough to know how to do that but not when it's appropriate or preferred

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

      Thanks Madeline, I'll add From into() the video idea list!

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

    Well done 👍

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

    What theme is this, I really need this. I think I'll probably die without this theme :)

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

      Sonokai! My current favorite...

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

      @@codetothemoon Thanks, that is awesome. Did you tweak the Colors or what?

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

      Can you share your version please

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

    Hey, there is one thing I'm wondering. Is it good or bad practice to return a polymorphic dyn error type? I had one side project recently where so many different types of errors could occur that I just ended up using Result

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

    Great video :D

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

    amazing content (as always)

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

    that was great! This is exactly something I struggled with as a Rust beginner but coming from other languages.

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

    9:20 I would use the previous variant since it makes it clear I just don't add anything to accum if there is an error. This way I'm adding something which might be zero. It's not as obvious and maybe even less performant.

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

    I've programmed c# for over 15 years, and rust only outside of work. I love your explainations of what's happening in rust.

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

      thanks, really happy you're getting something out of the videos!

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

    Thank You