I have know Idea how trait bound work in this case

impl <T:SuperBound,'a> MyTrait<'a> for MyStruct<'a> {
fn get(&self)->&'a str;
fn set(&mut self);
}
fn fn_required_superbound(&self,T){}
fn main(){
let s = Mystruct {}
s.set() // error :-> error:type annotations needed
   //cannot satisfy `... and required bound for set()
}

in this impl , only one func require a superbound. other doesn't.
but I can't separate set(), get() from this .
why can this happen ?
and how to fix it?

There's a lot off about the example and it's hard to guess what you were attempting.

Try sharing something closer to your actual code and the full error you get from running cargo build in a terminal.

1 Like

the error is at 35th line.

There are ways to be specific about the type without changing the trait, but as you can see they're awkward to use.

You probably want to move the generic to the method. But note:

  • Implementers have to handle every C: Certification<'_> if you do this, not just particular ones
  • This makes the trait not-dyn-compatible. If that matters, you could use a different signature
    fn is_certification(&self, c: &dyn Certification<'_>) -> bool;
    

If having to handle every C: Certification<'_> doesn't work for you, you could make a new trait for just that method.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.