December 5, 2022

Understanding of traits in rust programming language | RUST trait

rust programmer Nepal, Blockchain Development in Nepal, Blockchain Developer, Smart Contract Developer Nepal.

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

So here we are trying to explain the traits in the simplest way possible in my words.

What is a 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 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,
}

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 then implement the trait type. This is similar to the implementation function impl. For example, we have added the sample code for your convenience.

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}”};
}

In the above code example, we 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;
}
}

In the above block of code impl the keyword is used to implement the trait TotalMarks in the struct Marks. Inside the block, we have added a total function to carry out the addition of the marks, as we have defined the function signature in the TotalMarks trait above, in a similar fashion we can add as many 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.

NoteFrom 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 advanced concept and usages of trait in rust.

Author: Amit Khatri ” Blockchain Engineer | RUST | Solidity “