Rust Generics and Traits: Define Common Struct Behaviors 🦀
Вставка
- Опубліковано 8 чер 2024
- The Rust language provides a couple of important concepts called Traits and Generics. These two constructs work closely in conjunction with each other, to reduce the amount of code that's necessary to write software effectively. In this video, we will explore how Traits are defined, how to define Generic input arguments on Rust structs, and how to use Trait Bounds to limit which types can be used as Generic inputs.
🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
📖 Rust Traits: doc.rust-lang.org/book/ch10-0...
📖 Rust Generics: doc.rust-lang.org/book/ch10-0...
Visual Studio Code ➡️ code.visualstudio.com
Rust Website ➡️ rust-lang.org
Rustup Installer ➡️ rustup.rs
Rust Docs ➡️ doc.rust-lang.org/book
Please follow me on these other social channels!
➡️ trevorsullivan.net
➡️ github.com/pcgeek86
➡️ / pcgeek86
➡️ / trevorsullivan
➡️ / trevorsoftware
➡️ tiktok.com/pcgeek86
All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names,trademarks and brands does not imply endorsement.
#rustlang #rust #rustdev #opensource #software #linux #devops #programming #rusty #dev #coding #codinglife #code #coder #ubuntu #ubuntulinux #appdev #developer - Наука та технологія
Check out the FULL Rust video playlist! 🦀 ua-cam.com/play/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL.html
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.
One of the best explanations on traits out there! Thank you, Trevor!
Excellent, great clear explanation -> Thank You!
You really nailed the explanation Sir.
You way of explaining complex stuff is amazing! Thank you! Subscribed!
You're so kind! Thank you and I'm glad you're learning! 🥰🦀
Thank you so much! This was really great and helpful!
Great Stuff!!! Please keep on creating valuable content about Rust. Also, if you please create a video on efficient data structures in Rust.
thanks a lot for your work mate !❤
Best explanation dude!!
Great Exaplanation!
Awsome teaching
Thanks a lot mate, u r the best
I'm glad you're learning Rust!! 🦀
awesome video
Great !
Good sir you missed the implementation on the 'Person' animal.
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.
Thank you! I'm so glad that this series has helped you. Could you share a code example of what you're thinking?
@@TrevorSullivanenum PetType{Dog, Cat,Bear,Tiger} as the selection for your pet "type"?
Such a clear explanation. I like that you didn't just us This video deserves a lot more views. UA-cam, sort your algorithm!!
Thanks so much for your kind feedback! I'm glad you got something out of this video!
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?
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.
❤❤❤❤
Hey Trevor, so what would YOU use Rust to create? I am asking most ambitious software you can imagine using it for.
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?
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.
is there a github repo for the code examples ?
@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.
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?
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!!