Implementing Rust Traits

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

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

  • @Matt23488
    @Matt23488 Год назад +14

    The `From` trait is a really useful trait. If you didn't know, you can use the question mark operator (don't remember what its official name is in Rust) to auto return a None or Err which cleans up your code quite a bit if you want to pass the burden of handling the failure case to the caller. The problem is you may have multiple calls to external libraries that all return different error types, so you either have to make your function return a boxed trait object, or you can create your own error type (like you did in this video) and then you can implement From on your error type for each error you want to use the ? operator on.

    • @andrew-burgess
      @andrew-burgess  Год назад +1

      Thanks, that’s good to know! I’ll check out From as well.

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

      The official name is the question mark operator. source: rust reference

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

      Or you can simply use the crate “anyhow” to do that for you

  • @JavierHarford
    @JavierHarford Год назад +5

    Im really enjoying your Rust videos, clear and concise descriptions, thank you!

  • @sq5321
    @sq5321 Год назад +6

    i find it really cool that you share some tips about rust from a typescript mind, i would appreciate that you do more rust videos! i started learning rust a few days ago and those videos are really helpful

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

    Excellent tutorial. I've been trying to learn rust and find it useful to watch other people code while concisely explaining what they're writing as well as why. You've done a great job at this, so keep it up! Great to see examples of practical problems that arise and how to solve them using the standard library. Even though the video was about traits, you touched on other things in such a way that custom error types, and_then, and ok_or are all totally in my toolbelt now!

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

    I love this, Andrew! Please do more of this. And since many of your subscribers come from a TS background, please compare concepts from Rust to concepts from TS.

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

    yay, new episode. been liking this series so much

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

    Thank you 🙏🏼 seriously.

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

    man im just so excited about this rust series

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

    Instead of a str.parse, you could use Equation::from_str() as well, just saying so people understand better. str.parse is just a generic function on str where the generic is constrained to implement the FromStr trait, and it calls that implementation.

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

    More rust vids!

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

    This channel is going to be big.

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

    great showcase of how to implement a trait, but also how to use the hints of the compiler, rust docs and reading the explanations. How do you find all those methods / functions you needed? I'm also learning rust and find it sometimes very hard to find the correct function or if this function even exists.

    • @andrew-burgess
      @andrew-burgess  Год назад +1

      I find a lot of methods just be reading through the docs for the standard lib. It’s pretty thoroughly documented!

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

    btw, to be pedantic, I read that you would use .to_string() when the thing is something other than a string slice, and to_owned() when its a string slice. i think they both mostly work the same but you get just that extra little bit of safety with to_owned() on string slices

    • @andrew-burgess
      @andrew-burgess  Год назад

      Good to know, thanks!

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

      I thought the difference was really more about expressing the programmer's intent. Use to_owned() when you have a String or str that you don't own but want one for yourself. Use to_string() when you have something that isn't a String but need one. While the end result may be the same in some cases, which you pick helps a code reader (i.e. future you) know your intent.

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

      You could also use .into() or String::from() to do the conversion.

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

    Quick tip:
    Better not to use usize and isize as a value since it architecture dependent
    It still good for unsafe code, indexing array or to save some raw memory address
    But if number refers to a value, in your case, use u32 for unsigned and i32 for signed integer (or i/u 8, 16, 64, 128 for specific cases)

  • @Khl8122
    @Khl8122 7 месяцев назад +1

    How did you invoke “code actions” from rust-analyzer?

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

    Thanks, but parse and from_str are totally not the same? So, how is that happening. I see the note in the docs, about implicit, but unfortunately that is hand waved. Maybe you get to it, just at the beginning, but this is the part that is often the hardest to understand, because it is not explicit.

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

    Thanks for this video. I would like to know how you configured vim to work with Rust

  • @gertjanvandenbroek1620
    @gertjanvandenbroek1620 11 месяцев назад +1

    Hey I'm pretty new here, not an authority but i have a small question.
    In the video you parse your string like so:
    let value = s.parse::();
    But in the rust book they (afaik) always do it like so:
    let value : i32 = s.parse()
    Both work, and to my untrained eye it looks like the latter reads better.
    However, are these 2 exactly the same? Or is there a big benefit to the first one that I'm not seeing?

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

      for variables the choice is up to you but when you would rather have a value as an expression and not turn it into a variable and it need to infer a Type its the only way to infer the type

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

    Bad examples? You show bool, but that is just a fn, no impl for …. Makes seeing the trait a mystery. Thanks again

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

    For some reason I really like rust when watching someone write and explain it. When I try to write it, everything becomes...hectic. Isn't ParseError an error enum that you could extend with ParseEquationError?

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

    I suspect this is lunarvim that you are using, do you use it also for bigger projects or only for something like this? I've configured webstom/pycharm ideavim plugin, to have custom keybindings with all the leader stuff and easymotion working. After installing lunarvim my biggest problem was that there is massive amount of custom keybindings that are (for me) created only in lunarvim, maybe they are versatile and I just don't see it, because I am not really familliar with default keybindings in e.g. telescope. The second question is related to errors refreshing, I've realised that some errors like "no such function name in scope" are not refreshed automatically, user needs to save the file, is it something you did solve or you just save it every time?

    • @andrew-burgess
      @andrew-burgess  Год назад +2

      Yep, lunarvim is right! I use it for big projects at work as well as small personal stuff. I’ve learned the keybindings that I care about, and thats working for me for now.
      Regarding error refresh: I compulsively save any file I’m working on, so this hasn’t really been an issue for me.

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

    Pretty similar to Haskell's type classes

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

    Man Rust seems like an amazing language but for whatever reason I can never wrap my head around its syntax.
    Every time I watch a Rust video I see a new piece of syntax thats completely foreign to me.