Its a great cheatsheet. Thank you for putting it together. I am new to Rust and have to write a web service for an interview assignment. I am going to refer to this cheatsheet a lot.
Yeah. This is exactly what I've been thinking should happen for a long time. This is far superior to typical class inheritance. Also note that generics and interfaces/traits are two sides of the same coin. In other languages, traits/interfaces are runtime bound, demanding references, and generics are just compile-time interfaces. Arguably both paradigms are useful. compile-time is more efficient, runtime is more flexible (good for say, dynamic linking or MAYBE massive sets of generics).
It would be useful to have an overview of the common traits in the Rust standard library. Maybe that's another video I haven't seen yet. Bogdan, these are the best videos I've found for learning Rust. Your presentation is so organized and clear. Thank you.
Your videos are sort of lifeline, when you forgot how to swim. First of all, I'll as `nice developer` will read some topic from The Rust Book, but even as Python Middle Developer, sometimes it is really hard to understand almost "low-level" language principles. Second, I'll go to your channel and look through some videos about topic that I can't understand and this kind of union of The Rust Book and your videos make the whole information really nice structured in my head. Big thanks!
I sure appreciate your approach to explaining/showing the RUST language. There are no "for Dummies" type tangents. Just concise, plain examples presented simply followed on with variations of alternative and/or increasingly complex examples (along with quick dives behind instances of syntax sugar). To repeat the name of one of my favorite programming events: "No Fluff, Just Stuff"
I'm a sole dev on a project that's lasted about a year and a half. Rust is massive overkill but I'm introducing it anyway. It's my party and they can cry if they want to.
very google able. Self refers to the type, while self refers to the instance. There's a few more details because self could be self, mut self, or &self or &mut self, but thats the idea.
Nice video. When I saw the used examples, I was like I have seen this before. Checked my books, then Bingo! The Rust Book, which you mentioned in your video tag. Keep up the good work bro. Let us make rust rule the world for the next 40 years.
Rust Trait is a bit strange in its natural. e.g. You can't have a collection of some trait Vec but rather this weird looking Vec. The claim is the at compile time, the compiler cant possibly know the size of the trait. But trait itself could perhaps just be implemented as a fat-pointer, just like a Box, String, etc... But Trait as a concept is the core of the Rust language, I wish we can see a bit more detail talks in this area.
@@letsgetrustysure, but what I am saying is in another scenario, the compiler doesn’t have to know if it only box out a pointer of “a thing”. That’s how Go implemented their version of “trait”, called interface. But apparently, rust’s trait isn’t implemented that way.
@@jimxu1963 This would be like storing a void pointer in C. Useless unless you cast it back to some type later. Which type? That would have to be stored somewhere. That's what rust does for you. Your impression of how Go works under the hood, is seems wrong. The type has to be stored somewhere, if you want to do something with the data.
Thanks for the great content. I have a question - for the sake of better understanding of the new material for a Rust noob, can we say that Traits are smth like Interfaces and Struct + Impl is smth like a Class declaration + its body of functions implementations?
Thanks for your videos! I am a bit unclear on what the point of traits is. if you need to define the function in each struct you want to use the trait on anyway, why not just forgo the trait and impl the function?
I think traits are to force consistency of use and restrict the input/output types. When ‘summarize’ is a trait, the input parameter has to be self, and the output has to be a string. In a larger project, this can be nice because you can expect the summarize trait to “just work” as defined with less error checking. Especially if you have 10s-100s of different structs that you want to ‘summarize’. If you create the ‘summarize’ as an independent function of each, a user might then be uncertain about what ‘summarize’ is going to return or the params it needs. Maybe you made ‘summarize’ for Newspaper return a string (title), but ‘summarize’ for the Tweet actually returns a int (ex. total retweets).
hey, Do u have git repo which includes all the code samples been used in this playlist !! That will be quite helpful rather then looking into rust book .
In this particular example, isn’t it kinda redundant? Could i not just impl this same method for separate structs and it still be the same? What’s the advantage of the trait here really?
So there is no way to express "fn foo (a:T, b:U ) { ... }" without name repetition? Having two params of different type but implementing the same trait. Is there an idiom/syntax for it?
Any time you type impl Trait for Struct, you are defining functionality that is not "shared functionality", because the functionality is specific to that Struct. So if that's the case, why break method definitions up between different traits instead of implementing directly on the struct? It's less code, easier to read, and more straightforward to implement directly on the struct. I can easily write code using Traits because I understand how they work. But WHY would I write code this way? What am I missing?
Another helpful video for my Memorial Day 2022 Rust-fest. This was really importing subject matter for me. It tied together how Rust implements an alternative to OOP by using an "interface-like" model. Much more elegant for what we call "multiple inheritance" in C++. In fact, I give (C++)-- for this! So far Rust is making a lot of sense and I am enjoying it. I am having some arguments with the compiler though. I'll explain such a case below unless anyone else is suffering. I had some trouble typing in some of this code to try it out on the notify part. It seems I had one or more of the declarations above of the pub structs and traits. I found the compiler error messages completely misleading and useless. I had trouble with the call to notify. For some reason it kept giving me a vague error about a Type expected in the notify(item: &article) line on the word item. This too seemed to be related to declaration or some other voodoo reason because eventually I typed in notify(&article) without the item: first. Then VS Code 1.67.2 Universal with rust-analyzer extension installed auto-inserted the item:, same text as I had before, but no error. I think I need to understand why the order of declarations seemed to matter. It may have been a red herring and some other code issue was causing the problem. No matter what order I had them in, the compiler hadn't had a problem until some point in the evolution of the code as I went through this video. It's been over a year since you published this. I suspect occasionally certain language elements may have changed and that is leading to some of my troubles. I have mixed feelings on rust-analyzer extension to VS Code. I've had to battle with it a few times. One such case is I have code from an example and I want to modify it. Sometimes if I go to the end of an expression and delete the last thing so I can type in something different, it removes the type ascription or some other changes that I hadn't asked for and aren't correct based on my task. But overall it is worth using I think.
You know it would be nice if you didnt just regurgitate exactly what's in the book and didnt use examples from the book verbatim so that people who have read the book but still dont understand it can see the concepts used differently.
@@letsgetrusty The problem here then is that too many details from the book are skipped. If your going to base your videos on the book that's fine but explaining things (even more-so than the book) would be really helpful rather than just giving the tldr of the code. Which is just my interpretation of your videos, and I could be wrong my intention is to give you some constructive criticism.
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
Its a great cheatsheet. Thank you for putting it together. I am new to Rust and have to write a web service for an interview assignment. I am going to refer to this cheatsheet a lot.
Looks like interface and abstract class had a baby
I feel ya
Yeah it’s kinda like a hybrid version of concept and abstract class, in c++ language.
Yeah. This is exactly what I've been thinking should happen for a long time. This is far superior to typical class inheritance.
Also note that generics and interfaces/traits are two sides of the same coin. In other languages, traits/interfaces are runtime bound, demanding references, and generics are just compile-time interfaces. Arguably both paradigms are useful. compile-time is more efficient, runtime is more flexible (good for say, dynamic linking or MAYBE massive sets of generics).
I think they are called type classes, also used in haskell
Yes!!
These videos are really amazing. When I forget what I've read, or I'm simply bored with reading, it's so amazing to come back to these.
Sir, these videos are still gold. Every once in a while i come here to catch up on some topics.
You rock ✌🏼
It would be useful to have an overview of the common traits in the Rust standard library. Maybe that's another video I haven't seen yet.
Bogdan, these are the best videos I've found for learning Rust. Your presentation is so organized and clear. Thank you.
"Traits allow us to define a set of methods that are shared across different types" - I learned more at 01:10 than 20 mins of other Traits videos! 🔥
With each video I truly understand it gets cooler and cooler.
i'm happy that i can see more and more rust content on youtube finally
Your videos are so clear and helpful. Without your videos I can’t learn rust in such an quick and easy way. Thank you so much!
I've found good Rust documentation here and there, but this course is so well done!!!!! Thanks for all these videos 👍
Your videos are sort of lifeline, when you forgot how to swim.
First of all, I'll as `nice developer` will read some topic from The Rust Book, but even as Python Middle Developer, sometimes it is really hard to understand almost "low-level" language principles.
Second, I'll go to your channel and look through some videos about topic that I can't understand and this kind of union of The Rust Book and your videos make the whole information really nice structured in my head.
Big thanks!
Hell yess, Bogdan! These explanations rule! I was trying to wrap my head around traits for the last hour
I sure appreciate your approach to explaining/showing the RUST language.
There are no "for Dummies" type tangents.
Just concise, plain examples presented simply followed on with variations of alternative and/or increasingly complex examples (along with quick dives behind instances of syntax sugar).
To repeat the name of one of my favorite programming events:
"No Fluff, Just Stuff"
Thank you for the feedback!
This is so much better format than reading the rust-lang book
The rust book is well written I don't agree
To me, this feels more difficult than when I was learning the rust ownership. :(
The Rust Book's chapter on Traits hurts....
true lol. i found ownership to be ezz af. but things like enums and traits and all that stuff feels way harder bruh. my eyes just start rolling
@@QmVuamFtaW4 it gets easier .. in time haha. Just hang in there!
I'm a sole dev on a project that's lasted about a year and a half. Rust is massive overkill but I'm introducing it anyway. It's my party and they can cry if they want to.
I cannot express how valuable your stuff is lol. You have seriously done such amazing work with these videos and playlist as a whole!
If you could do a video on "Self" vs "self" and explain the difference in usage that would be amazing :))
very google able. Self refers to the type, while self refers to the instance. There's a few more details because self could be self, mut self, or &self or &mut self, but thats the idea.
Nice video. When I saw the used examples, I was like I have seen this before. Checked my books, then Bingo! The Rust Book, which you mentioned in your video tag. Keep up the good work bro. Let us make rust rule the world for the next 40 years.
Rust Trait is a bit strange in its natural. e.g. You can't have a collection of some trait Vec but rather this weird looking Vec. The claim is the at compile time, the compiler cant possibly know the size of the trait. But trait itself could perhaps just be implemented as a fat-pointer, just like a Box, String, etc... But Trait as a concept is the core of the Rust language, I wish we can see a bit more detail talks in this area.
Vec is a vector of "things" that implement the Display trait. The compiler does not know the size of those "things".
@@letsgetrustysure, but what I am saying is in another scenario, the compiler doesn’t have to know if it only box out a pointer of “a thing”. That’s how Go implemented their version of “trait”, called interface. But apparently, rust’s trait isn’t implemented that way.
@@jimxu1963 This would be like storing a void pointer in C. Useless unless you cast it back to some type later. Which type? That would have to be stored somewhere. That's what rust does for you. Your impression of how Go works under the hood, is seems wrong. The type has to be stored somewhere, if you want to do something with the data.
TH-cam should allow you to click on the like button more than once, like 1M times; I love your videos, thanks!
Thanks mate, really appreciate this series, i have learned a lot
thank you , very helpful for me, second day to rust , love it already
Not conceptually difficult, but so many syntaxes make this topic more challenging to remember than ownership and borrowing.
Good attempt at these videos.. Thanks to u, Lang book is fastened up nicely !!!
This was super useful. Thanks for breaking it down so clearly.
Cool stuff, as usual :) . Wonder, are there any Covariance/Contravariance generics aspects implemented in Rust
I'm really enjoying this language.
Today I've learnt 2 things: traits in Rust and that bad lighting makes you look like panda :D Great series BTW!
Thanks for a great video! I have one question. Why don't we use 'for' keyword when impl Pair?
Thanks for the great content. I have a question - for the sake of better understanding of the new material for a Rust noob, can we say that Traits are smth like Interfaces and Struct + Impl is smth like a Class declaration + its body of functions implementations?
The content is pure gold! Thank you very much
Great videos. Learning a lot.
Solid and concise video, thanks
The font size you use is just the optimal to watch on both mobile and desktop
Excellent video. Same concept as Java interfaces.
Love this feature ❤
I am a C developer and at this chapter Rust started to look like black magic!
Beautiful explanation, subscribed.
tldr traits are just interfaces but different
@4:48 you point Tweet's summarize implemantation as the default implementation.
Very clear! Thank you.
I would've implemented Display instead of making a custom trait, its more widely supported especially in println
Thanks for your videos! I am a bit unclear on what the point of traits is. if you need to define the function in each struct you want to use the trait on anyway, why not just forgo the trait and impl the function?
I think traits are to force consistency of use and restrict the input/output types. When ‘summarize’ is a trait, the input parameter has to be self, and the output has to be a string. In a larger project, this can be nice because you can expect the summarize trait to “just work” as defined with less error checking. Especially if you have 10s-100s of different structs that you want to ‘summarize’.
If you create the ‘summarize’ as an independent function of each, a user might then be uncertain about what ‘summarize’ is going to return or the params it needs. Maybe you made ‘summarize’ for Newspaper return a string (title), but ‘summarize’ for the Tweet actually returns a int (ex. total retweets).
It's for allowing polymorphism. It's supposed to be like interfaces in other languages lol
Thanks a so much. Instantly subscribed to the channel!
So Traits really are Type Classes in Haskell. right?
@1:26 imagine you wanted to define a trait that can be implemented by other class but mark it as private
Excellent. 'Nuff said.
hey, Do u have git repo which includes all the code samples been used in this playlist !! That will be quite helpful rather then looking into rust book .
I don’t unfortunately. Might do that in the future
Top content. Thanks dude
In this particular example, isn’t it kinda redundant? Could i not just impl this same method for separate structs and it still be the same? What’s the advantage of the trait here really?
Fast track rust book
Newbie here. Without the examples in this video I was not able to get the code from the book to work.
yup that what I wanted to see, the use where keyword in generics, btw do you have music playing in your background
Yes but it's quieter in this video.
So there is no way to express "fn foo (a:T, b:U ) { ... }" without name repetition? Having two params of different type but implementing the same trait. Is there an idiom/syntax for it?
i just want to do operator overloading a+b and a+=b for objects but still not explain but found an example here
Thanks, mate.
bro, what extensions in vscode for rust you use?
Any time you type impl Trait for Struct, you are defining functionality that is not "shared functionality", because the functionality is specific to that Struct. So if that's the case, why break method definitions up between different traits instead of implementing directly on the struct? It's less code, easier to read, and more straightforward to implement directly on the struct. I can easily write code using Traits because I understand how they work. But WHY would I write code this way? What am I missing?
Another helpful video for my Memorial Day 2022 Rust-fest. This was really importing subject matter for me. It tied together how Rust implements an alternative to OOP by using an "interface-like" model. Much more elegant for what we call "multiple inheritance" in C++. In fact, I give (C++)-- for this! So far Rust is making a lot of sense and I am enjoying it. I am having some arguments with the compiler though. I'll explain such a case below unless anyone else is suffering.
I had some trouble typing in some of this code to try it out on the notify part. It seems I had one or more of the declarations above of the pub structs and traits. I found the compiler error messages completely misleading and useless. I had trouble with the call to notify. For some reason it kept giving me a vague error about a Type expected in the notify(item: &article) line on the word item. This too seemed to be related to declaration or some other voodoo reason because eventually I typed in notify(&article) without the item: first. Then VS Code 1.67.2 Universal with rust-analyzer extension installed auto-inserted the item:, same text as I had before, but no error. I think I need to understand why the order of declarations seemed to matter. It may have been a red herring and some other code issue was causing the problem. No matter what order I had them in, the compiler hadn't had a problem until some point in the evolution of the code as I went through this video.
It's been over a year since you published this. I suspect occasionally certain language elements may have changed and that is leading to some of my troubles. I have mixed feelings on rust-analyzer extension to VS Code. I've had to battle with it a few times. One such case is I have code from an example and I want to modify it. Sometimes if I go to the end of an expression and delete the last thing so I can type in something different, it removes the type ascription or some other changes that I hadn't asked for and aren't correct based on my task. But overall it is worth using I think.
So it's like interfaces in c#
Thanks
where is chapter 13?? Please add them in the title. finding relevant vids is a challenge
Now what is display for?
What theme is that
what a beatifull language gosh!
What is similar to interface{} in go for rust
So, a trait is like interface.
Why is 9:15 not allowed? They both impl the trait.
5:48 did you just wake up 😅
I really dosen't undrestand usage of traits? where we beed to use them?
Your discord server?
THREE syntaxes for adding required traits to function arguments?
that's 2 too many.
This looks very complicated. I might take some time to get used to it.
wrg
Just implement classes already 😂
I'm missing your flag.
You know it would be nice if you didnt just regurgitate exactly what's in the book and didnt use examples from the book verbatim so that people who have read the book but still dont understand it can see the concepts used differently.
That would be nice but the point of this series is to be the video version of the book. Videos on specific topics like traits will be coming though.
@@letsgetrusty The problem here then is that too many details from the book are skipped. If your going to base your videos on the book that's fine but explaining things (even more-so than the book) would be really helpful rather than just giving the tldr of the code. Which is just my interpretation of your videos, and I could be wrong my intention is to give you some constructive criticism.