Issues with Trait


#1

Hi Folks

I am having an issue with traits. Here is some code:

pub struct Foo <'a>{
    foo: &'a str
}
pub trait Bar {
    fn baz() -> Something;
}

impl <'a> Bar for Foo <'a> {
    fn baz() -> Something {
   // some legit code here
    }
  }

(I have encapsulated some code, so kindly ignore any syntax or formatting issues)

But i get the error:

error[E0599]: no method named baz found for type Foo<'_> in the current scope

Any ideas?


#3

If Bar is another module, you need use another_module::Bar to make it applicable.

Rust allows multiple traits with the same method to be implemented on the same type. It avoids potential ambiguity by requiring explicit use of traits.


#4

Thanks. Yeah I am aware of that.