DEV Community

Amit Khatri
Amit Khatri

Posted on

Basic understanding of traits in rust programming language | RUST trait

Image description

What is this all about?

This is a short and basic tutorial of RUST’s trait, it is the topic that is quite troublesome for those who come from languages like Python, Javascript and so on, also this feature is new to the programmers coming from other programming languages too. We can find the good code examples and ideas about traits from RUST official documentation here, The Rust Book. But for me the documentation was just a bunch of letters and sentences as the language was totally new to me and I was hardly getting a hang of it, though the code examples and syntax were of great help to me and I highly recommend you to follow the official documentation for once or twice.

So here I am trying to explain the traits in the simplest way possible in my words.

What is Trait in RUST?

It is the way to define the shared behavior, it tells the compiler about the functionality which type provides.

Traits we have used

We have mostly used traits as Debug, Clone, Copy these are the common and we have attributes that automatically do it. So on using #[deribe(Debug, Clone)] we automatically implement Debug and Clone traits in Struct or Enum.

#[derive(Debug)]
    struct Student{
    number: uszie,
}
Enter fullscreen mode Exit fullscreen mode

But in some other cases we have to implement traits manually using impl keyword. I will be adding more on impl keyword below.

How to use trait?

Usually in traits, we first have to define the method signature and them implement the trait type. This is similar to implementation function impl . For example, I have added the sample code for your convenient.

struct Marks {
    math: f32,
    science: f32,
    english: f32,
}
trait TotalMarks {
    fn total(&self) -> f64;
}
impl TotalMarks for Marks{
    fn total(&self) ->f64{
        let total = self.math + self.scienct +self.english;
        total;
    }
}
fn main(){
    let bob = Marks{
        math:40.0,
        science:45.0,
        english:42.0,
     };
let bob_marks = bob.total();
    println!(“total marks of Bob {total_marks}”};
}
Enter fullscreen mode Exit fullscreen mode

In the above code example I have created the Marks struct and total as the trait signature for TotalMarks which I have used for the Marks struct to calculate the total marks provided in the Marks struct.

impl TotalMarks for Marks{
    fn total(&self) ->f64{
        let total = self.math + self.scienct +self.english;
        total;
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above block of code impl keyword is used to implement the trait TotalMarks in the struct Marks. Inside the block I have added total function to carry out the addition of the marks, as I have defined the function signature in TotalMarks trait above, in the similar fashion we can add as may functions as we require to implement for the given structs or enum.

While defining traits, we can simply define the function signature also rather than the function, doing so we can generalize the function for the program if needed, but if that is not required we can add the logic of adding the marks in the trait function also.

Note: From RUST 1.58.0v format string can capture arguments, so we can write {data} in the string.

So, that’s all about the basics of trait in rust you can go through the official documentation and youtube videos to learn more about the advance concept and usages of trait in rust.

Top comments (0)