Error Handling in Rust

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 85

  • @letsgetrusty
    @letsgetrusty  3 года назад +7

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

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

      Didn't the russian propagandists say that there is no panic :)
      Probably their error handling is not well implemented.

  • @albertz0
    @albertz0 3 года назад +71

    Great content. Coming from mainly Fortran and a bit C++, Rust is like heaven.

    • @ShimoriUta77
      @ShimoriUta77 3 месяца назад +1

      You're a goddamn hero! Fortran is not for the faint of heart.

  • @uovo
    @uovo 2 года назад +15

    Your series basically flattened the learning curve of rust.

  • @user-yk1lz7gb2t
    @user-yk1lz7gb2t 2 года назад +18

    I expected to see the usual try-catch blocks that we are mostly used to but Rust has exceeded the expectations that I had in mind before watching this video. I have mostly worked with js and python. It is very difficult to deal with errors in both languages.

  • @KyleHarrisonRedacted
    @KyleHarrisonRedacted Год назад +16

    This was incredibly useful, for such an ambiguous and huge topic you’ve really booked it down to simply approaches and even answered by accident a bunch of questions I had about Rust in general, and boiled that down to less than 17min

  • @lardycake
    @lardycake 2 года назад +10

    Just want to say thank you for all of your vids. I'm a senior software engineer with experience in most of this kind of stuff in other languages and you go at exactly the right pace to be able to pick up Rust quickly.

  • @CalifornianViking
    @CalifornianViking 2 года назад +10

    Bogden, your videos are great. I have read the Rust book, but find that your videos are a lot easier to follow.
    You are asking about what other content to cover. A lot of people that start using Rust come from class and object oriented languages such as C#, Java, and TypeScript. It would be great to have videos about how to architect your code and comparing how things were done in the other languages to how it is done in Rust. One example is polymorphism which is not obvious since there is no class hierarchy.

  • @miro42344234
    @miro42344234 Год назад +2

    watching this playlist while reading the book, I got to say, job really well done, makes everything much clearer and easier to grasp, which says a lot, considering the fact that the book is well written

  • @ZachPeters
    @ZachPeters 3 года назад +48

    You need more subscribers! Your content is amazing

  • @katarsloto4223
    @katarsloto4223 2 года назад +19

    Love your content, it's makes it clear and easier than any other sources that i used to learn rust, keep it up

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

    I watch these videos first and then read the chapter. Has been working great.

  • @ccprt1275
    @ccprt1275 2 года назад +8

    These videos are brilliant, thanks for your hard work putting them together.

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

    the book is so verbose that reading the entire book became a pain for me. your videos literally became my life saviour by helping me continue learning Rust without taking the pain the book has to give! Thank You Very much for your videos!

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

    Concise and high quality. Thanks.

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

    Thank you so much for all your videos. High quality content !

  • @sakuranooka
    @sakuranooka 2 года назад +4

    So, in the last example ("Guess"), if, instead of panicking, I wanted to create and return a Result structure, what would the code look like?

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

      Instead of panicking you'd return an Err("Invalid guess") and instead of returning a Guess on success, wrap in in an Ok(). Then reflect the new return type in the signature: -> Result
      Instead of the error being a String/&str you can use a custom error type.

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

    Your videos are amazing, keep up the good work

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

    Great video!! 👍, I want to ask u a general question, as u might know js/ts and there we have try-catch-finally blocks, safe navigation operator and nullish coalescing operator that helps the code to be more safer, as a rustacean how do u feel about error handling in rust and js, and what do u think which one is actually better??

  • @yasserelidrissiameur4723
    @yasserelidrissiameur4723 3 года назад +1

    Your content is amazing

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

    Hi Bogdan, im learning Rust. I love your content! I came across combinators (ok_or, unwrap_or, ...) in Rust. I don't know which is applicable for different cases. hope you will release a video to guide us on this. thanks :)

    • @letsgetrusty
      @letsgetrusty  2 года назад +6

      Ah yes combinators are a good topic for a video. Will make one eventually but not for a while.

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

      @@letsgetrusty coming from a Javascript background, what is confusing to me about Rust are the combinators and Traits. I understand combinators and Traits are meant to help programmers. for a newcomer though, it is hard to know which type of combinator and Trait to use in which case. I wonder how many Rust learners face this situation as well

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

    Spasibo wam balshoy!!

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

    Why does my Result causes compilation error stating "expected 0 generic arguments" ?

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

    Love the content and ALSO love the background :)

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

    This helped so much

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

    As of 05/14/2024 "when I attempted to code along, your (9:52) line 6 and line 17 doesn't work anymore. miss-matched type error of when now it needs to be so, seems like people who watch this as of me writing this need to ignore that example.

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

    Hello Bogdan, at 15:40, you created a public value() method so that an instance of Guess struct cannot access the underlying value property. But we still seem to have access to the value property from the instance variable. Am I understanding it right or confusing it with classic OOP?

    • @v1k70r99
      @v1k70r99 Год назад +2

      In the video "Structs in Rust" he explains that even if a struct is set to be public, all fields inside the struct are private by default. This can be changed by adding "pub" in front of the field.

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

    Great video. Thanks

  • @elmondo.
    @elmondo. 2 года назад

    Thanks for this tutorial

  • @tech3425
    @tech3425 17 дней назад

    Loved it

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

    thanks

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

    you missed to explain the syntax/meaning of | error | function/closure

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

    we was reading from `f` so why that needed to be converted to mut 10:43?

  • @timothyvandyke9511
    @timothyvandyke9511 3 года назад

    Glad I found this

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

    A follow up video on thiserror and anyhow would be good

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

    @ 15:44, do you think making a Enum with ValidGuess(i32) and InvalidGuess where new returns a instance of that Enum would be better for handling a validation state instead?

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

      If you want to return something to the caller that helps it deal with a failure, that sounds like what he said about error propagation. So, maybe, returning a Result would suffice? This way the caller can handle it, just like with your suggestion of an Enum, since there are just two cases, one that is Ok and one that is an Error... (I know I'm very late on the answer, just putting this here so new people can see, I guess 😅.)

  • @initfunction6961
    @initfunction6961 3 года назад

    I'm waiting for a videos about data structures :)

  • @itscozy6279
    @itscozy6279 3 года назад +1

    how did u get the inline parameters to show up btw nice videos keep it up

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

      I installed the rust-analyzer VS code extension: marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer

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

    I'm confused why you need a mutable File to read_to_string, but you can use an _immutable_ File to read_to_end? Why must it be immutable in one instance but not the other?

    • @Gramini
      @Gramini Год назад +2

      It must be mutable in both cases if I'm not mistaken. The documentation for read_to_end says it wants a mutable File: fn read_to_end(&mut self, buf: &mut Vec)
      The reason why it has to be mutable although you're only reading is because reading changes the cursor/position of the file. The file itself might not be mutable, but the variable and data on of the program is.

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

      @@Gramini You're absolutely right. I saw a tutorial that seemed to *not* mark the variable as mutable, but now I can't remember where I saw it. Looking at the documentation of File, it's clear that both functions require a mutable reference to self (for the reasons you mentioned, very likely).
      Thanks for the reply. Have a great day.

  • @sahilverma4077
    @sahilverma4077 3 года назад +3

    I like the implementation of Result enum but I still miss try-catch, it feels so like home to me, I mean you don't need to know much about the error or anything you can just provide a case and if it doesn't work you have a catch that will run for sure, and we can even have multiple lines in try
    cause I think it's tedious to do use Result everytime and the other alternatives are not at all reliable, I liked the lambda somewhat but we still have to manage all the errors together, and sorry if I'm talking naively, I'm from a java background and am learning rust only for web assembly (and i hate C++)

    • @letsgetrusty
      @letsgetrusty  3 года назад +5

      Yeah I hear you Sahil. Rust definitely makes error handling more tedious. You can use unwrap() to avoid error handling while prototyping code.

    • @31redorange08
      @31redorange08 3 года назад +1

      Sounds like a code smell to me if you don't know which errors can occur and just catch 'em all.

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

      @@31redorange08 python try except go brrr
      It allows you to catch specific errors you would like!

    • @Anon.G
      @Anon.G Год назад

      ​@@Terroidit allows you to, but you can easily just catch them all.

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

      🏳️‍🌈?

  • @DavidJames-hg5tj
    @DavidJames-hg5tj 2 года назад

    There is a whining noise in the background. Since I can hear it on both a Linux computer and an Android phone it has to part of this series of videos. I understand you-tube has an option to remove background noise.

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

    Bogdan is a Romanian name. Are you Romanian-American?

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

    It was a lot of stuff packed in 16 minutes. I need to digest it.

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

    I assume that when the File instance gets Dropped, it closes the file. In other languages, the programmer must explicitly close the file.

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

      That's correct. The borrow checker knows when a file is no longer needed and drops it, which causes the handle to close.

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

    Can you teach Serde?

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

    unwrap_or_else() seems convoluted. I'm sure the Rust designers have their reasons but I think even a modern systems level language could be more a bit more elegant.

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

      "or_else" lets you specify a lazily-evaluated call that will run to create a valid result or option only if needed, unlike "or", which always generates the valid value regardless of whether it is needed. So you can use "or" when the default value is a constant or some value you already have available, and "or_else" when you actually need to do some work to generate it.
      In either case, the value after this call will always be Ok/Some, so you can prepend "unwrap_" to get it right away while you are sure it will not panic. If you had to call unwrap separately, it would be incorrectly flagged by the linter as a place where the code might panic.

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

      If you want somethin to run only in case of an error you can also use if let, as in
      if let Err(error) = might_give_an_error() {
      println!("There's an error: {error}");
      }

  • @mztikk
    @mztikk 3 года назад

    whats the outro music?

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

      soundcloud.com/rantalaofficial/heroes

    • @mztikk
      @mztikk 3 года назад

      @@letsgetrusty thanks a lot

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

    but why are you using ...... default aah linux theme, use catpuccin

  • @michaelnajera7958
    @michaelnajera7958 3 года назад

    So what country ya from, Bogdan?

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

      Ukraine

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

      @@letsgetrusty Praying for you right now 🙏

  • @user-oq1hp9xq4g
    @user-oq1hp9xq4g Год назад

    The audio and video is kinda out of sync.

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

    It's sad that I can only give this video 1 like

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

    Ahhhh Rust is hard !!! from a js developer

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

    Your flag is backwards.

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

    I liked the video but really too bad that you're stopping at some specific chapters and you say "this is in chapter xx, we will cover that another time".
    I was expecting a video that covers entirely error handling. If the video is restricted to some specific chapters I might as well go read the book directly...
    This is just a friendly suggestion. I like your content but that would be great if you could make some videos a bit different than the Rust book

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

    YOU FORGOT TO TURN ON THE LETS GET RUSTY SIGN!!!!

  • @GolangDojo
    @GolangDojo 3 года назад +1

    I heard it isn't a real programming language if JetBrain doesn't have an IDE for it

    • @letsgetrusty
      @letsgetrusty  3 года назад +8

      www.jetbrains.com/rust/

    • @GolangDojo
      @GolangDojo 3 года назад

      @@letsgetrusty That's a plugin boi

    • @ozanmuyes
      @ozanmuyes 3 года назад +1

      To be honest I stopped using JetBrains products right after I moved from PHP and let me tell you it was one of my radical life decisions and it was a blessing for me

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

      @@GolangDojo A good read 😉 fasterthanli.me/articles/i-want-off-mr-golangs-wild-rid

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

      @@GolangDojowelp, they made an ide for it

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

    Thanks for the lesson. (FYI your U.S. flag is displayed backwards! 🫡)