Rust Programming Tutorial #28 - Defining Traits

preview_player
Показать описание
Similar to interfaces in other languages, traits allow you to set 'rules' or 'requirements' for a particular struct in order for it to be considered to be 'something'.

In this video we create a trait called 'HasVoiceBox' which says that any Struct implementing this trait must have a speak() and can_speak() method defined on it.

For more information refer to the Rust documentation:

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

I don't need traits for what is shown in this example, I can just use regular impl's. The real purpose of traits is that I can declare a function whose arguments must implement a trait, regardless of the type of the argument, like this:

fn listen_to_voicebox(item: &impl VoiceBox)

The argument "item" can be of any type as long as that type implements VoiceBox, hence the definition of listen_to_voicebox() has the guarantee that the functions speak() and can_speak() are indeed callable on item. It's like the concept of an interface in Java.

Any didactical example that intends to explain the essential purpose of traits should necessarily define at least two different types, e.g. Person and Robot, that both implement VoiceBox, among other - and differing - impl's.

sakuranooka
Автор

I really like these tutorials, but maybe explain better what traits actually does, compared to structs / instances.

mtothem
Автор

Do we really need traits ? I think impl is enough to do this

enhboldotgonbaatar
Автор

super basic and crystal clear to understand . keep doing them .

sitcomrave
Автор

why you should implement traits for a specific struct when you can only implement that one struct?

samas
Автор

Why we don't use : Impl Struct{ ...} without traits ? What'is the advantage by using traits? Thanks a lot

dev-rachid
Автор

I don't get it. I can do all that with struct-methods as well. So what is the trait for?

Canaris
Автор

Awesome tutorials, Continue making them !!!

siddharthnaithani
Автор

Can you have default functions with traits. Like an extension or inheritance

elijahlucian
Автор

It's like pure virtual function in C++ right?

sbn
Автор

So Dom, these are essentially / similar to virtual functions in c++... Same sort of idea... If you assign / have the trait => you have to define its actual method for your circumstance?

jonathanmoore
Автор

I don't understand traits. I mean, we create a trait with some methods but then when we implement the trait into the struct, we still define them there, too. We define those methods in both traits and the impl, why? If we use traits, I guess it's for reusability. I.e. when we want same methods into two different structs, instead of repeating the code 2 times, we define those methods into a trait and implement the trait into both structs. But here, methods are written two times, what am I missing? Anyone, please?

RusuTraianCristian
Автор

So traits are pure virtual functions in C++ or interfaces in Java?

AbhishekNigam
Автор

How did Bob speak in the end? He is 0 years old and cannot speak.

Tvde
Автор

I don’t speak English and until that moment everything was clear. But we can do it without trait. It is not clear why it is needed.

atommixz
Автор

dude can u post the code so i can copy & paste ./

juancastillo
Автор

Hi, great tutorial.
Can you also explain life time and life time parameters?

taimoorkhan
Автор

I can do the same without trait HasVoiceBox, so, this is a bad and incomprehensible example. He does not explain why the necessary traits.

struct Person {
name: String,
age: u8
}

impl Person {
fn speak(&self) {
println!("Hello, my name is {}", self.name);
}

fn can_speak(&self) -> bool {
if self.age > 0 {
return true;
} return false;
}
}

fn main() {
let person = Person {
name: String::from("Bob"),
age: 41
};

println!("Can {} speak? {}", person.name, person.can_speak());
person.speak();
}

atommixz
Автор

I think fn can_speak() should better have been

if self.age > 1 {
true
else
false
}

alokswaincontact