是否可以在 trait 定义中使用 `impl Trait` 作为函数的返回类型?

编程入门 行业动态 更新时间:2024-10-26 14:38:55
本文介绍了是否可以在 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` 作为函数的返回类型?

本文发布于:2023-05-29 21:34:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/353336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   定义   类型   trait   Trait

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!