Traits in Rust

Поділитися
Вставка
  • Опубліковано 1 гру 2024

КОМЕНТАРІ • 104

  • @letsgetrusty
    @letsgetrusty  3 роки тому +9

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

    • @sshovelyjoe
      @sshovelyjoe Рік тому

      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 роки тому +164

    Looks like interface and abstract class had a baby

    • @sahilverma4077
      @sahilverma4077 3 роки тому +2

      I feel ya

    • @jimxu1963
      @jimxu1963 3 роки тому +2

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

    • @SemiMono
      @SemiMono 2 роки тому +10

      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 2 роки тому +7

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

    • @vincent-thomas
      @vincent-thomas 2 роки тому +1

      Yes!!

  • @gleep83
    @gleep83 3 роки тому +63

    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 2 роки тому +30

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

  • @ireallylikecoffee14
    @ireallylikecoffee14 Місяць тому +1

    Sir, these videos are still gold. Every once in a while i come here to catch up on some topics.
    You rock ✌🏼

  • @dmdeemer
    @dmdeemer Рік тому +9

    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.

  • @alinmgheorghe
    @alinmgheorghe 7 місяців тому +1

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

  • @linjianru
    @linjianru 2 роки тому +14

    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!

  • @phantasm4037
    @phantasm4037 3 роки тому +25

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

  • @jeremietamburini
    @jeremietamburini Рік тому +3

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

  • @ypucandeleteit
    @ypucandeleteit 3 роки тому +6

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

  • @ziyadkader6767
    @ziyadkader6767 2 роки тому +6

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

  • @mihunt3r929
    @mihunt3r929 7 місяців тому

    The content is pure gold! Thank you very much

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

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

  • @rohitcr6839
    @rohitcr6839 6 місяців тому

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

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

  • @anindyasundarmanna6683
    @anindyasundarmanna6683 3 роки тому +59

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

    • @Nounearthlyglow
      @Nounearthlyglow Рік тому +4

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

    • @QmVuamFtaW4
      @QmVuamFtaW4 Рік тому +3

      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!

  • @ZZDryno
    @ZZDryno 2 роки тому +3

    Great videos. Learning a lot.

  • @tonhom260
    @tonhom260 Рік тому

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

  •  3 роки тому +2

    Solid and concise video, thanks

  • @markmcdonnell
    @markmcdonnell 3 роки тому +1

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

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

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

  • @max_ishere
    @max_ishere 2 роки тому +15

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

    • @bnchi
      @bnchi 8 місяців тому +1

      The rust book is well written I don't agree

  • @jimxu1963
    @jimxu1963 3 роки тому +2

    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  3 роки тому +2

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

    • @jimxu1963
      @jimxu1963 3 роки тому

      @@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 3 роки тому +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.

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

  • @codeAbinash
    @codeAbinash 8 місяців тому

    Love this feature ❤

  • @luismoriguerra669
    @luismoriguerra669 8 місяців тому

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

  • @vs_gaming3013
    @vs_gaming3013 Рік тому

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

  • @dmytrobortnichuk8501
    @dmytrobortnichuk8501 2 роки тому +2

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

  • @samu350
    @samu350 2 роки тому

    Beautiful explanation, subscribed.

  • @meka4996
    @meka4996 3 роки тому +1

    Very clear! Thank you.

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

  • @dawid_dahl
    @dawid_dahl 2 роки тому

    Thanks a so much. Instantly subscribed to the channel!

  • @Gruby7C1h
    @Gruby7C1h 2 роки тому

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

  • @bbkr2063
    @bbkr2063 6 місяців тому

    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?

  • @techbytefrontier
    @techbytefrontier 9 місяців тому

    I'm really enjoying this language.

  • @BigBeesNase
    @BigBeesNase 2 роки тому +1

    So Traits really are Type Classes in Haskell. right?

  • @theof6757
    @theof6757 3 роки тому +1

    Top content. Thanks dude

  • @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 Рік тому

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

  • @_Redu
    @_Redu Рік тому

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

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

  • @Shabazza84
    @Shabazza84 3 місяці тому

    Excellent. 'Nuff said.

  • @Khl8122
    @Khl8122 11 місяців тому

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

  • @caasieu
    @caasieu Рік тому

    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?

  • @mathcritic
    @mathcritic 9 місяців тому

    Excellent video. Same concept as Java interfaces.

  • @alpsavasdev
    @alpsavasdev Рік тому

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

  • @x.liquilite.x5435
    @x.liquilite.x5435 Рік тому

    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?

  • @peppidesu
    @peppidesu 2 роки тому +1

    tldr traits are just interfaces but different

  • @footballCartoon91
    @footballCartoon91 2 роки тому

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

  • @archangel9582
    @archangel9582 2 роки тому

    Now what is display for?

  • @PieroRolando
    @PieroRolando 3 роки тому

    bro, what extensions in vscode for rust you use?

  • @whoman0385
    @whoman0385 6 місяців тому

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

  • @Sunil-yz7ig
    @Sunil-yz7ig Рік тому

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

  • @tudorminciunescu9614
    @tudorminciunescu9614 Рік тому

    What theme is that

  • @adrian_franczak
    @adrian_franczak Рік тому

    So it's like interfaces in c#

  • @footballCartoon91
    @footballCartoon91 Рік тому

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

  • @meanmole3212
    @meanmole3212 3 роки тому

    Thanks, mate.

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

  • @larryd9577
    @larryd9577 Рік тому

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

  • @davidmcbride5357
    @davidmcbride5357 2 роки тому

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

  • @JitenPalaparthi
    @JitenPalaparthi 2 роки тому

    What is similar to interface{} in go for rust

  • @ehsankhorasani_
    @ehsankhorasani_ 2 роки тому

    Thanks

  • @mikemardis
    @mikemardis 9 місяців тому

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

  • @tech3425
    @tech3425 3 місяці тому

    Fast track rust book

  • @oussamasethoum2755
    @oussamasethoum2755 2 роки тому

    So, a trait is like interface.

  • @yousefsaddeek
    @yousefsaddeek 5 місяців тому

    5:48 did you just wake up 😅

  • @hoax-777
    @hoax-777 3 місяці тому +1

    where is chapter 13?? Please add them in the title. finding relevant vids is a challenge

  • @icaroamorim3123
    @icaroamorim3123 2 роки тому +1

    what a beatifull language gosh!

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

  • @Singlton
    @Singlton 9 місяців тому

    Your discord server?

  • @StratAlpha
    @StratAlpha 19 годин тому

    Great Video but The notify pub fn notify(item1: &T, item2: &T) did not work for me but this worked instead: pub fn notify(item1: &T, item2: &U){

  • @teslainvestah5003
    @teslainvestah5003 2 роки тому

    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.

  • @ollydix
    @ollydix Рік тому

    Just implement classes already 😂

  • @zes7215
    @zes7215 Рік тому

    wrg

  • @johnteran8889
    @johnteran8889 2 роки тому

    I'm missing your flag.

  • @arthurtapper1092
    @arthurtapper1092 3 роки тому

    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  3 роки тому +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 3 роки тому +2

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