Це відео не доступне.
Перепрошуємо.
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/
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.
Thanks, that’s good to know! I’ll check out From as well.
The official name is the question mark operator. source: rust reference
Or you can simply use the crate “anyhow” to do that for you
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
same with me!
Im really enjoying your Rust videos, clear and concise descriptions, thank you!
yay, new episode. been liking this series so much
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!
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.
man im just so excited about this rust series
This channel is going to be big.
Thank you 🙏🏼 seriously.
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.
I find a lot of methods just be reading through the docs for the standard lib. It’s pretty thoroughly documented!
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.
More rust vids!
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
Good to know, thanks!
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.
You could also use .into() or String::from() to do the conversion.
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)
How did you invoke “code actions” from rust-analyzer?
Thanks for this video. I would like to know how you configured vim to work with Rust
I’m using the lunarvim configs.
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.
Bad examples? You show bool, but that is just a fn, no impl for …. Makes seeing the trait a mystery. Thanks again
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.
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?
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
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?
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.
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?
Pretty similar to Haskell's type classes