@fuzail
Traits in Rust 🦀
Traits define a set of methods that a type must implement. Think of them as contracts that a type must fulfill. Using traits, you can write functions and methods that operate on many different types, as long as they implement the specific trait.
Why Use Traits?
Polymorphism: Write generic code that can operate on any type that implements the trait.
Code Reuse: Define shared behavior in a trait and implement it for multiple types.
Abstraction: Abstract away implementation details, focusing on what a type can do rather than how it does it.
In the below example:
I have defined a `Summary` trait with a single method summarize.
I have implemented this trait for the `Article` struct.
I have created a `notify` function that can accept any type of implementation of the `Summary` trait, allowing for polymorphism.