Implementing Rust Traits

  Рет қаралды 13,258

Andrew Burgess

Andrew Burgess

Күн бұрын

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 Жыл бұрын
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 Жыл бұрын
Thanks, that’s good to know! I’ll check out From as well.
@ciso
@ciso Жыл бұрын
The official name is the question mark operator. source: rust reference
@rendezone
@rendezone Жыл бұрын
Or you can simply use the crate “anyhow” to do that for you
@JavierHarford
@JavierHarford Жыл бұрын
Im really enjoying your Rust videos, clear and concise descriptions, thank you!
@sq5321
@sq5321 Жыл бұрын
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
@irlshrek
@irlshrek Жыл бұрын
same with me!
@tmangner
@tmangner Жыл бұрын
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.
@joshdotmp4
@joshdotmp4 Жыл бұрын
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!
@apidas
@apidas Жыл бұрын
yay, new episode. been liking this series so much
@irlshrek
@irlshrek Жыл бұрын
man im just so excited about this rust series
@Gruby7C1h
@Gruby7C1h Жыл бұрын
This channel is going to be big.
@hugodsa89
@hugodsa89 Жыл бұрын
Thank you 🙏🏼 seriously.
@cooldude3010
@cooldude3010 Жыл бұрын
More rust vids!
@PaulSebastianM
@PaulSebastianM Жыл бұрын
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.
@tobiasweyer5063
@tobiasweyer5063 Жыл бұрын
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 Жыл бұрын
I find a lot of methods just be reading through the docs for the standard lib. It’s pretty thoroughly documented!
@wlockuz4467
@wlockuz4467 Жыл бұрын
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.
@Khl8122
@Khl8122 5 ай бұрын
How did you invoke “code actions” from rust-analyzer?
@irlshrek
@irlshrek Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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)
@JohnPywtorak
@JohnPywtorak Жыл бұрын
Bad examples? You show bool, but that is just a fn, no impl for …. Makes seeing the trait a mystery. Thanks again
@gertjanvandenbroek1620
@gertjanvandenbroek1620 9 ай бұрын
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 9 ай бұрын
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
@walis85300
@walis85300 Жыл бұрын
Thanks for this video. I would like to know how you configured vim to work with Rust
@andrew-burgess
@andrew-burgess Жыл бұрын
I’m using the lunarvim configs.
@JohnPywtorak
@JohnPywtorak Жыл бұрын
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.
@fixer8173
@fixer8173 Жыл бұрын
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 Жыл бұрын
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 Ай бұрын
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 Жыл бұрын
Pretty similar to Haskell's type classes
old TypeScript syntax I just discovered
8:54
Andrew Burgess
Рет қаралды 3,8 М.
How to Start Learning Rust (for TypeScript Developers)
9:49
Andrew Burgess
Рет қаралды 8 М.
WHO DO I LOVE MOST?
00:22
dednahype
Рет қаралды 22 МЛН
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 20 МЛН
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 51 МЛН
Tom & Jerry !! 😂😂
00:59
Tibo InShape
Рет қаралды 50 МЛН
Rust Data Modelling Without Classes
11:25
No Boilerplate
Рет қаралды 163 М.
how TypeScript 5.5 makes safer types in fewer keystrokes
7:48
Andrew Burgess
Рет қаралды 1,8 М.
Save neovim files automatically with auto-save.nvim
6:32
Traits in Rust
11:44
Let's Get Rusty
Рет қаралды 95 М.
Generic Types in Rust
15:18
Let's Get Rusty
Рет қаралды 65 М.
Rust Powered Polymorphism ⚡️ With Traits
9:55
Code to the Moon
Рет қаралды 91 М.
Rust & Wasm
9:38
No Boilerplate
Рет қаралды 187 М.
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 157 М.
Rust for TypeScript Developers by ThePrimeagen | Preview
14:59
Frontend Masters
Рет қаралды 96 М.
💅🏻Айфон vs Андроид🤮
0:20
Бутылочка
Рет қаралды 690 М.
Lid hologram 3d
0:32
LEDG
Рет қаралды 6 МЛН
Will the battery emit smoke if it rotates rapidly?
0:11
Meaningful Cartoons 183
Рет қаралды 30 МЛН
Cadiz smart lock official account unlocks the aesthetics of returning home
0:30