Rust Generics and Traits: Define Common Struct Behaviors 🦀

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 พ.ย. 2024

ความคิดเห็น • 42

  • @TrevorSullivan
    @TrevorSullivan  ปีที่แล้ว +3

    Check out the FULL Rust video playlist! 🦀 th-cam.com/play/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL.html

  • @meka4996
    @meka4996 19 วันที่ผ่านมา +1

    This is the BEST video on Rust Trait! I finally understand Traits and Generics go together. Thank you!

    • @TrevorSullivan
      @TrevorSullivan  19 วันที่ผ่านมา +1

      I'm so happy to hear that you learned something from this video! I appreciate your kind comment! Enjoy writing Rust code! 🦀🦀

  • @abbcc555
    @abbcc555 ปีที่แล้ว +6

    This really solidified my understanding. By far the best explanation. Your examples and pace is perfect, also the fact that you didn't conform to the typical(pun intended) use of T and U etc.

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars หลายเดือนก่อน +1

    Trevor wonderfully explained. Do you have video on lifetimes and if not are you planning to make one?
    Thanks again :)

    • @TrevorSullivan
      @TrevorSullivan  หลายเดือนก่อน +1

      @@a_maxed_out_handle_of_30_chars thanks so much for your kind comment! I really appreciate it. I don't have any videos on lifetimes specifically. They still kind of confuse me. 😊

  • @sval4020
    @sval4020 7 หลายเดือนก่อน +2

    You way of explaining complex stuff is amazing! Thank you! Subscribed!

    • @TrevorSullivan
      @TrevorSullivan  7 หลายเดือนก่อน

      You're so kind! Thank you and I'm glad you're learning! 🥰🦀

  • @wasabinator
    @wasabinator หลายเดือนก่อน

    You present very well I must say. Thanks for making these videos.

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars ปีที่แล้ว +1

    Hello, sorry I still don't understand traits
    why can't we use the below code?
    struct Pet {
    first_name: String,
    pet: T,
    }
    struct Dog {}
    impl Dog {
    fn make_sound(self) {
    println!("bark!");
    }
    }
    struct Cat {}
    impl Cat {
    fn make_sound(self) {
    println!("meow!");
    }
    }
    fn main() {
    let dog1 = Dog {};
    let cat1 = Cat {};
    let p1 = Pet {
    first_name: "dog".to_string(),
    pet: dog1,
    };
    let p2 = Pet {
    first_name: "cat".to_string(),
    pet: cat1,
    };
    p1.pet.make_sound();
    p2.pet.make_sound();
    }
    why do we need traits when using implementation block works?

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +2

      That's a great question! Generics and Traits offer some similar functionality. However, the benefit with traits is that you can focus on object behaviors rather than the types themselves. In your example, a crocodile also makes a sound, but a crocodile is not a pet. So if you need to access common behavior across dogs, cats, and crocodiles, using the Pet type doesn't make sense, because crocodiles aren't pets. Instead, you would define a trait called "MakesSound" (or whatever name you want) and then declare the make_sound() function in that trait.
      There's nothing wrong with using generics, as long as they fit your use case. Traits just give you another mechanism to deal with different types.
      I hope this helps!!

  • @lordmelbury7174
    @lordmelbury7174 10 หลายเดือนก่อน +3

    Such a clear explanation. I like that you didn't just us This video deserves a lot more views. TH-cam, sort your algorithm!!

    • @TrevorSullivan
      @TrevorSullivan  10 หลายเดือนก่อน

      Thanks so much for your kind feedback! I'm glad you got something out of this video!

  • @Esmairidem
    @Esmairidem หลายเดือนก่อน +1

    Great explanation, thanks bro

  • @eltonrr1
    @eltonrr1 ปีที่แล้ว +2

    One of the best explanations on traits out there! Thank you, Trevor!

  • @scottb4029
    @scottb4029 ปีที่แล้ว +1

    Trevor, could you implement an enum as a trait for the pets and have the enum have all of the pets? Also, I wanted to say, thank you. This might be the best video on traits and generics I have ever seen.

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +2

      Thank you! I'm so glad that this series has helped you. Could you share a code example of what you're thinking?

    • @scottb4029
      @scottb4029 11 หลายเดือนก่อน

      ​​@@TrevorSullivanenum PetType{Dog, Cat,Bear,Tiger} as the selection for your pet "type"?

  • @jasonbraithwaite9204
    @jasonbraithwaite9204 5 หลายเดือนก่อน

    Excellent, great clear explanation -> Thank You!

  • @skull_cyber
    @skull_cyber 6 หลายเดือนก่อน

    Im confuse in rust concepts from enums to everyother. Your videos are great when i try to implement these on my side it gets hard. can you tell me some exercises which helps me clearing concepts in rust about each topic?

    • @TrevorSullivan
      @TrevorSullivan  6 หลายเดือนก่อน +1

      Hello thanks for sharing your experience with Rust. I would recommend taking what you learn in these videos and adapt it to a different use case. For example, if I use an animal as an example, change it to a vehicle instead. Or a plane with departure / arrival times and locations, or a train that has a schedule. Make sense? Just think of something in the real world and then try to model it using Rust structs and methods.

  • @mykolagogol1192
    @mykolagogol1192 3 หลายเดือนก่อน

    thank you so much, you really helped me a lot!

  • @moose304
    @moose304 11 หลายเดือนก่อน

    Thank you so much! This was really great and helpful!

  • @AM-hc5vo
    @AM-hc5vo 5 หลายเดือนก่อน

    Great Stuff!!! Please keep on creating valuable content about Rust. Also, if you please create a video on efficient data structures in Rust.

  • @Andrew_.
    @Andrew_. ปีที่แล้ว

    thanks a lot for your work mate !❤

  • @gautamjh
    @gautamjh 9 หลายเดือนก่อน

    Great Exaplanation!

  • @azystar4563
    @azystar4563 8 หลายเดือนก่อน

    Good sir you missed the implementation on the 'Person' animal.

  • @princemarkied8071
    @princemarkied8071 ปีที่แล้ว

    Hey Trevor, so what would YOU use Rust to create? I am asking most ambitious software you can imagine using it for.

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +2

      Most ambitious? I'm not sure about that, but for starters, think about some CLI tools you could use. What about building web APIs, to call from automation scripts? For example, a TUI (Terminal User Interface) that helps you manage Kubernetes clusters, maybe? What kinds of technologies do you work with, and what could you simplify?

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +1

      Benchmarking tools would be another great use case for Rust, since it's a high performance language. For example, build a Postgres or MySQL benchmarking tool to compare performance in different configurations.

  • @bothwellw
    @bothwellw 6 หลายเดือนก่อน

    is there a github repo for the code examples ?

    • @TrevorSullivan
      @TrevorSullivan  6 หลายเดือนก่อน +1

      @bothwellw Unfortunately no, I don't have a GitHub repository for the samples. I encourage people to write the code out for themselves, to learn how things work. It's good exercise.

  • @alishmod530
    @alishmod530 6 หลายเดือนก่อน

    Awsome teaching

  • @rasi_rawss
    @rasi_rawss 2 หลายเดือนก่อน

    Superb!

  • @aar021
    @aar021 ปีที่แล้ว +1

    You really nailed the explanation Sir.

  • @ExidifulCrypto
    @ExidifulCrypto ปีที่แล้ว

    Thanks a lot mate, u r the best

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +2

      I'm glad you're learning Rust!! 🦀

  • @gokulrashtrami4340
    @gokulrashtrami4340 ปีที่แล้ว +1

    Best explanation dude!!

  • @nhwhn
    @nhwhn 8 หลายเดือนก่อน

    awesome video

  • @kurkdebraine8139
    @kurkdebraine8139 7 หลายเดือนก่อน

    Great !

  • @MH-bw9lh
    @MH-bw9lh ปีที่แล้ว +1

    ❤❤❤❤

  • @ojoawojoshua780
    @ojoawojoshua780 3 หลายเดือนก่อน

    But some dogs are dangerous 😭