Type parameter `T` must be covered by another type when it appears before the first local type

brief code as below:

use std::ops::Add;

struct A{
    val: String
}

type 
impl From<i64> for A {
    fn from(c: i64) -> Self {
        A{
            val: c.to_string()
        }
    }
}

//it warns: Type parameter `T` must be covered by another type when it appears before the first local 
impl<T: Into<A>> Add<A> for T {
    type Output = A;
    fn add(self, rhs: A) -> Self::Output {
        if let A { val:x } = rhs{
            A { val: self.to_string + x }
        }
    }
}

fn main(){

}

I have many kinds of objects to be Add to struct A, for those objects I impl the From to A for them.
I didn't want to write Add for each type of those objects, I wanna generics to deal with it.
how to avoid this error,? thanks advanced.

This rule is in place to prevent conflicting trait implementations from being defined in multiple places. You can make this work by switching T and A, but it will always require the A instance to be on the left side of +:

use std::ops::Add;

struct A{
    val: String
}

impl From<i64> for A {
    fn from(c: i64) -> Self {
        A {
            val: c.to_string()
        }
    }
}

impl<T: Into<A>> Add<T> for A {
    type Output = A;
    fn add(mut self, rhs: T) -> Self::Output {
        self.val.push_str(rhs.into().val.as_str());
        self
    }
}
3 Likes

Thanks ,

The concrete rules are here, though you may also need to refer to the rest of the RFC for a precise understanding of what constitutes an uncovered type.

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.