本文介绍了是否可以在 trait 定义中使用 `impl Trait` 作为函数的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在特征内定义具有 impl Trait 返回类型的函数?我想创建一个可以由多个结构实现的特征,以便所有结构的 new() 函数返回一个对象,它们都可以以相同的方式使用,而无需编写特定的代码给每一个.

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 structs so that the new() functions of all of them returns an object that they can all be used in the same way without having to write code specific to each one.

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

但是,我收到以下错误:

However, I get the following error:

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/lib.rs:2:17
  |
2 |     fn new() -> impl A;
  |                 ^^^^^^

这是impl Trait当前实现的限制还是我使用错误?

Is this a limitation of the current implementation of impl Trait or am I using it wrong?

推荐答案

如果您只需要返回当前正在实现 trait 的特定类型,您可能正在寻找 Self.

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;
}

例如,这将编译:

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<S: Into<String>>(name: S) -> Self;
    fn get_name(&self) -> String;
}

struct Person {
    name: String
}

impl A for Person {
    fn new<S: Into<String>>(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<S: Into<String>>(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<T: A>(a: &T) -> String {
    a.get_name()
}

游乐场

作为旁注.. 我在这里使用了 String 来支持 &str 引用.. 以减少对显式生命周期的需求和潜在的焦点丢失关于手头的问题.我相信在借用内容时返回 &str 引用通常是惯例,这在这里似乎很合适..但是我不想过多地分散实际示例的注意力.

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.

这篇关于是否可以在 trait 定义中使用 `impl Trait` 作为函数的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:28