Це відео не доступне.
Перепрошуємо.

Implementing Rust Traits

Поділитися
Вставка
  • Опубліковано 18 сер 2024
  • The next step in my rust journey is implementing a trait! Join me to learn about string parsing in rust.
    This is part of a new series I'm starting, where I - a TypeScript developer - learn rust!
    My setup: shaky.sh/tools/

КОМЕНТАРІ • 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

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

  • @JavierHarford
    @JavierHarford Рік тому +5

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

  • @apidas
    @apidas Рік тому +2

    yay, new episode. been liking this series so much

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

  • @irlshrek
    @irlshrek Рік тому +1

    man im just so excited about this rust series

  • @Gruby7C1h
    @Gruby7C1h Рік тому +1

    This channel is going to be big.

  • @hugodsa89
    @hugodsa89 Рік тому +1

    Thank you 🙏🏼 seriously.

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

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

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

  • @walis85300
    @walis85300 Рік тому +2

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

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

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

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

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

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

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

  • @DjLeonSKennedy
    @DjLeonSKennedy Рік тому +2

    Pretty similar to Haskell's type classes