Is it possible to use `impl Trait` as a function's return type in a trait definition?

前端 未结 4 1081
有刺的猬
有刺的猬 2020-11-28 15:45

Is it at all possible to define functions inside of traits as having impl Trait return types? I want to create a trait that can be implemented by multiple stru

4条回答
  •  醉酒成梦
    2020-11-28 16:28

    If you only need to return the specific type for which the trait is currently being implemented, you may be looking for Self.

    trait A {
        fn new() -> Self;
    }
    

    For example, this will compile:

    trait A {
        fn new() -> Self;
    }
    
    struct Person;
    
    impl A for Person {
        fn new() -> Person {
            Person
        }
    }
    

    Or, a fuller example, demonstrating using the trait:

    trait A {
        fn new>(name: S) -> Self;
        fn get_name(&self) -> String;
    }
    
    struct Person {
        name: String
    }
    
    impl A for Person {
        fn new>(name: S) -> Person {
            Person { name: name.into() }
        }
    
        fn get_name(&self) -> String {
            self.name.clone()
        }
    }
    
    struct Pet {
        name: String
    }
    
    impl A for Pet {
        fn new>(name: S) -> Pet {
            Pet { name: name.into() }
        }
    
        fn get_name(&self) -> String {
            self.name.clone()
        }
    }
    
    fn main() {
    
        let person = Person::new("Simon");
        let pet = Pet::new("Buddy");
    
        println!("{}'s pets name is {}", get_name(&person), get_name(&pet));
    }
    
    fn get_name(a: &T) -> String {
        a.get_name()
    }
    

    Playground

    As a side note.. I have used String here in favor of &str references.. to reduce the need for explicit lifetimes and potentially a loss of focus on the question at hand. I believe it's generally the convention to return a &str reference when borrowing the content and that seems appropriate here.. however I didn't want to distract from the actual example too much.

提交回复
热议问题