Learning Rust Trait

Why learning rust trait, I noticed something significant with the compiler.
First of all, I also want to say that the compiler is very helpful in resolving errors.

I noticed that adding semicolon to my code gave me an error i wasn't expecting:

| fn name(&self) -> &'static str{
| ______________________________^
46 | | self.name;
| | - help: consider removing this semicolon
47 | | }
| |
^ expected reference, found ()
|
= note: expected type &'static str
found type ()

But after removing it, the code works great. please can someone explain why the semicolon was throwing that error, because am coming in from a C programming background (PHP,JS) and semicolon is the way to go.

here is my code that works:

 struct Dog{
    name: &'static str,
    mood: &'static str,
    breed: &'static str
}

trait Animal {
    fn new(name: &'static str) -> Self;

    fn name(&self) -> &'static str;
    fn mood(&self) -> &'static str;
    fn breed(&self) -> &'static str;
    fn noise(&self) -> &'static str;

    fn sound(&self){
        println!("{} a {} dog {} when {}", self.name(), self.breed(),self.noise(),self.mood());
    }
}


impl Animal for Dog{
    fn new(name: &'static str) -> Dog {
        Dog {
            name: name,
            mood: "happy",
            breed: "local"
        }
    }

    fn name(&self) -> &'static str{
        self.name
    }

    fn mood(&self) -> &'static str{
        self.mood
    }

    fn breed(&self) -> &'static str{
        self.breed
    }

    fn noise(&self) -> &'static str{
        if self.mood == "happy"{
            "bark"
        }
        else{
            "whine"
        }
    }
}

fn main() {
    println!("Hello");
    let bingo: Dog = Animal::new("Bingo");
    bingo.sound();
}

Omitting the semicolon in Rust code means "return this value". If you want a semicolon, you need to use return:

fn name(self) -> &str {
    return self.name;
}

The book has a chapter on returning values from functions.

Note: You may find that using &'static str is inconvenient, as the only way to get a value of this type, is to have it be a hard-coded literal in the code. You should use the type String when storing a string somewhere, and you can use &str when you want to borrow a string (i.e. returning a reference to the contents without moving ownership). The book also has a chapter on strings.

PS. Prefer to use code blocks for compiler errors, as they don't mess up the spacing.

3 Likes

@alice that is to say that using the arrow function, rust was expecting a return value which is suppose to be the last expression on the code block, but due to the semicolon no value was return and makes the code invalid.

Thanks again.

Yes, to be exact, it said that by using -> &'static str, you declare that it returns a &'static str, but it found the type (), which is the name for the void type in Rust, and the () type is implicitly returned from functions with no explicit return.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.