Traits in Rust

Поделиться
HTML-код
  • Опубликовано: 14 июн 2024
  • The ultimate Rust lang tutorial. Follow along as we go through the Rust lang book chapter by chapter.
    📝 Get notified when the Rust Cheatsheet comes out: letsgetrusty.com/cheatsheet
    The Rust book: doc.rust-lang.org/stable/book/​
    0:00 Intro
    0:23 Defining Traits
    1:58 Implementing Traits
    2:55 Default Implementations
    5:01 Trait Bounds
    8:09 Returning Types the Implement Traits
    9:23 Conditionally Implement Methods
    10:27 Blanket Implementations
    10: 53 Outro
    #letsgetrusty​ #rust​lang #tutorial
  • НаукаНаука

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

  • @letsgetrusty
    @letsgetrusty  3 года назад +7

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

    • @sshovelyjoe
      @sshovelyjoe 10 месяцев назад

      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.

  • @GolangDojo
    @GolangDojo 3 года назад +136

    Looks like interface and abstract class had a baby

    • @sahilverma4077
      @sahilverma4077 3 года назад +2

      I feel ya

    • @jimxu1963
      @jimxu1963 2 года назад +1

      Yeah it’s kinda like a hybrid version of concept and abstract class, in c++ language.

    • @SemiMono
      @SemiMono 2 года назад +9

      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).

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

      I think they are called type classes, also used in haskell

    • @vincent-thomas
      @vincent-thomas Год назад +1

      Yes!!

  • @gleep83
    @gleep83 2 года назад +60

    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.

  • @learning_rust
    @learning_rust Год назад +25

    "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! 🔥

  • @dmdeemer
    @dmdeemer Год назад +7

    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.

  • @phantasm4037
    @phantasm4037 3 года назад +24

    i'm happy that i can see more and more rust content on youtube finally

  • @user-pm4vd6ij8i
    @user-pm4vd6ij8i 2 года назад +12

    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!

  • @ypucandeleteit
    @ypucandeleteit 3 года назад +5

    Hell yess, Bogdan! These explanations rule! I was trying to wrap my head around traits for the last hour

  • @anindyasundarmanna6683
    @anindyasundarmanna6683 2 года назад +59

    To me, this feels more difficult than when I was learning the rust ownership. :(

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

      The Rust Book's chapter on Traits hurts....

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

      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

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

      @@QmVuamFtaW4 it gets easier .. in time haha. Just hang in there!

  • @Tsundaere
    @Tsundaere Месяц назад

    With each video I truly understand it gets cooler and cooler.

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

    I've found good Rust documentation here and there, but this course is so well done!!!!! Thanks for all these videos 👍

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

    I cannot express how valuable your stuff is lol. You have seriously done such amazing work with these videos and playlist as a whole!

  • @mihunt3r929
    @mihunt3r929 Месяц назад

    The content is pure gold! Thank you very much

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

    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!

  • @ziyadkader6767
    @ziyadkader6767 2 года назад +6

    Thanks mate, really appreciate this series, i have learned a lot

  • @markmcdonnell
    @markmcdonnell 2 года назад +1

    This was super useful. Thanks for breaking it down so clearly.

  • @max_ishere
    @max_ishere 2 года назад +14

    This is so much better format than reading the rust-lang book

    • @bnchi
      @bnchi 3 месяца назад +1

      The rust book is well written I don't agree

  • @rohitcr6839
    @rohitcr6839 Месяц назад

    Good attempt at these videos.. Thanks to u, Lang book is fastened up nicely !!!

  • @adiguntimothy8625
    @adiguntimothy8625 3 года назад +2

    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.

  • @luismoriguerra669
    @luismoriguerra669 2 месяца назад

    RUclips should allow you to click on the like button more than once, like 1M times; I love your videos, thanks!

  • @ZZDryno
    @ZZDryno 2 года назад +3

    Great videos. Learning a lot.

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

    If you could do a video on "Self" vs "self" and explain the difference in usage that would be amazing :))

    • @IvanMalison
      @IvanMalison Год назад +7

      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.

  •  2 года назад +2

    Solid and concise video, thanks

  • @Christobanistan
    @Christobanistan 3 года назад +3

    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.

  • @meka4996
    @meka4996 2 года назад +1

    Very clear! Thank you.

  • @samu350
    @samu350 2 года назад

    Beautiful explanation, subscribed.

  • @tonhom260
    @tonhom260 9 месяцев назад

    thank you , very helpful for me, second day to rust , love it already

  • @theof6757
    @theof6757 3 года назад +1

    Top content. Thanks dude

  • @codeAbinash
    @codeAbinash 2 месяца назад

    Love this feature ❤

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

    Cool stuff, as usual :) . Wonder, are there any Covariance/Contravariance generics aspects implemented in Rust

  • @RobertLBarnard
    @RobertLBarnard 3 года назад +3

    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"

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

    Thanks a so much. Instantly subscribed to the channel!

  • @Khl8122
    @Khl8122 5 месяцев назад

    Not conceptually difficult, but so many syntaxes make this topic more challenging to remember than ownership and borrowing.

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

    I'm really enjoying this language.

  • @TheProsvetitelq
    @TheProsvetitelq 2 года назад +1

    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?

  • @meanmole3212
    @meanmole3212 2 года назад

    Thanks, mate.

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

    Today I've learnt 2 things: traits in Rust and that bad lighting makes you look like panda :D Great series BTW!

  • @vs_gaming3013
    @vs_gaming3013 8 месяцев назад

    Thanks for a great video! I have one question. Why don't we use 'for' keyword when impl Pair?

  • @ehsankhorasani_
    @ehsankhorasani_ 2 года назад

    Thanks

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

    Excellent video. Same concept as Java interfaces.

  • @jimxu1963
    @jimxu1963 2 года назад +1

    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.

    • @letsgetrusty
      @letsgetrusty  2 года назад +2

      Vec is a vector of "things" that implement the Display trait. The compiler does not know the size of those "things".

    • @jimxu1963
      @jimxu1963 2 года назад

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

    • @tsg1zzn
      @tsg1zzn 2 года назад +1

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

  • @PieroRolando
    @PieroRolando 2 года назад

    bro, what extensions in vscode for rust you use?

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

    The font size you use is just the optimal to watch on both mobile and desktop

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

    tldr traits are just interfaces but different

  • @BigBeesNase
    @BigBeesNase 2 года назад +1

    So Traits really are Type Classes in Haskell. right?

  • @Sunil-yz7ig
    @Sunil-yz7ig 11 месяцев назад

    I am a C developer and at this chapter Rust started to look like black magic!

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

    @4:48 you point Tweet's summarize implemantation as the default implementation.

  • @sahilverma4077
    @sahilverma4077 3 года назад

    yup that what I wanted to see, the use where keyword in generics, btw do you have music playing in your background

    • @letsgetrusty
      @letsgetrusty  3 года назад +1

      Yes but it's quieter in this video.

  • @bjugdbjk
    @bjugdbjk 2 года назад +1

    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 .

    • @letsgetrusty
      @letsgetrusty  2 года назад

      I don’t unfortunately. Might do that in the future

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

    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?

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

      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).

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

      It's for allowing polymorphism. It's supposed to be like interfaces in other languages lol

  • @caasieu
    @caasieu 7 месяцев назад

    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?

  • @whoman0385
    @whoman0385 19 дней назад

    I would've implemented Display instead of making a custom trait, its more widely supported especially in println

  • @bbkr2063
    @bbkr2063 Месяц назад

    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?

  • @archangel9582
    @archangel9582 2 года назад

    Now what is display for?

  • @tudorminciunescu9614
    @tudorminciunescu9614 10 месяцев назад

    What theme is that

  • @astaghfirullahalzimastaghf3648

    i just want to do operator overloading a+b and a+=b for objects but still not explain but found an example here

  • @x.liquilite.x5435
    @x.liquilite.x5435 7 месяцев назад

    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?

  • @astaghfirullahalzimastaghf3648

    @1:26 imagine you wanted to define a trait that can be implemented by other class but mark it as private

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

    What is similar to interface{} in go for rust

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

    So it's like interfaces in c#

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

    Newbie here. Without the examples in this video I was not able to get the code from the book to work.

  • @davidmcbride5357
    @davidmcbride5357 2 года назад

    I really dosen't undrestand usage of traits? where we beed to use them?

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

    Why is 9:15 not allowed? They both impl the trait.

  • @richsadowsky8580
    @richsadowsky8580 2 года назад

    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.

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

    So, a trait is like interface.

  • @Singlton
    @Singlton 4 месяца назад

    Your discord server?

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

    what a beatifull language gosh!

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

    THREE syntaxes for adding required traits to function arguments?
    that's 2 too many.

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

    This looks very complicated. I might take some time to get used to it.

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

    wrg

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

    Just implement classes already 😂

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

    I'm missing your flag.

  • @arthurtapper1092
    @arthurtapper1092 2 года назад

    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
      @letsgetrusty  2 года назад +3

      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.

    • @arthurtapper1092
      @arthurtapper1092 2 года назад +1

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