Is &self parameter necessary in the function of trait?

curlywei

Looking this example code

trait Animal {
    fn make_sound(&self);
}

struct Dog;
impl Animal for Dog {
    fn make_sound(&self) {
        println!("Woof!");
    }
}

struct Cat;
impl Animal for Cat {
    fn make_sound(&self) {
        println!("Meow!");
    }
}

fn main() {
    let dog = Dog;
    let cat = Cat;
    dog.make_sound();
    cat.make_sound();
}

The function(method) make_sound() is just print a string.

I check the content of this function, make_sound() does not seems to invoke any information of Cat struct.

So I tried to remove &self parameter, but could not get the pass compiler.

Is &self necessary in the function of trait? Why?

Or where is my understanding wrong?

Edit: Add code which could not get the pass compiler

trait Animal {
    fn make_sound();
}

struct Dog;
impl Animal for Dog {
    fn make_sound() {
        println!("Woof!");
    }
}

struct Cat;
impl Animal for Cat {
    fn make_sound() {
        println!("Meow!");
    }
}

fn main() {
    let dog = Dog;
    let cat = Cat;
    dog.make_sound();
    cat.make_sound();
}
kmdreko

No, a self parameter is not required for trait functions. However, if there is no self parameter, you can't use the obj.func() syntax since there is no instance to be provided. You have to do Type::func():

trait Animal {
    fn make_sound();
}

struct Dog;
impl Animal for Dog {
    fn make_sound() {
        println!("Woof!");
    }
}

struct Cat;
impl Animal for Cat {
    fn make_sound() {
        println!("Meow!");
    }
}

fn main() {
    Dog::make_sound();
    Cat::make_sound();
}
Woof!
Meow!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is parameter in self executing function necessary?

Trait with function without "self" as parameter cannot be made into an object

Why is it necessary to place self as a parameter for method decorators?

Understanding the 'self' parameter in the context of trait implementations

How to make trait method take &Self parameter

Rc of trait object as function parameter

Necessary trait bounds on generic function implementing nom parser

Passing boxed trait object to function accepting generic parameter implementing the trait

The type parameter is not constrained by the impl trait, self type, or predicates

Passing trait's funcion as a parameter to another function

Why is 'self' required to be a function parameter?

Why can a function on a trait object not be called when bounded with `Self: Sized`?

Why is the `Sized` bound necessary in this trait?

Is is necessary to have success as a parameter in Ajax call if my function is not returning anything?

JavaScript: Self-executing function with parameter

Use type of self as function parameter in extension

I want to thread a function with the self parameter

Passing in "self" as a parameter in function call in a class (Python)

Make Box<Self> function parameter mutable

Declaring Associated Type of Trait Object in Async Function Parameter

Define a trait with a function that returns an associated type with the same lifetime as one parameter

How to delegate an async function with non-static parameter by a trait?

What are the differences between an impl trait argument and generic function parameter?

Type trait that checks whether function parameter can be initialized by a certain type

Implement trait for slice with lifetime bound from function parameter

Why can't I declare a trait function with tuple parameter matching?

Error in creating a function with a trait-bounded parameter wrapped in Option enum

Refering to self type of a trait

Why can't Box<dyn Trait> be pased to a function with &mut Trait as parameter