A trait defines the functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way. We can use trait bounds to specify that a generic type can be any type that has certain behavior.
Traits are similar to a feature often called interfaces in other languages, although with some differences.
Struct
Define a struct – in this case Langauge struct with fields and data types
struct Language{
name: String,
stable_version: f32,
launched_year: u32
}
Define Trait and implemenation
Define trait Description with represent method. Implement trait Description for Language struct.
trait Description{
fn represent(&self) -> String;
}
impl Description for Language{
fn represent(&self) -> String{
format!("the language {} was lauched in {} year and current stable version is {}", self.name, self.launched_year, self.stable_version)
}
}
Main Block
Instantiate Langauge and call the represent method implemented for Struct
{
let pro_langauge_rust = Language{
name: String::from("Rust"),
stable_version: 1.84,
launched_year: 2012
};
println!("Programming language is {}", pro_langauge_rust.represent())
}
Output
Programming language is the language Rust was lauched in 2012 year and current stable version is 1.84
Default Trait
In the previous example Trait just has the definition but no implementation. Define a default implementation to the trait and no remove the implementaion for Langauge
trait Description{
fn represent(&self) -> String{
String::from("is the langauge you should learn.")
}
}
impl Description for Language{
// No implementation
}
fn main{
let pro_langauge_rust = Language{
name: String::from("Rust"),
stable_version: 1.84,
launched_year: 2012
};
println!("{} {}", pro_langauge_rust.name, pro_langauge_rust.represent());
}
Output
Rust is the langauge you should learn.