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;
| ^^^^^^
Is this a limitation of the current implementation of impl Trait
or am I using it wrong?
impl Trait
. For instance, you couldn't add a method that inPerson
returns aPet
but inPet
returns aPerson
, although both implementA
. The RFC (1522) mentions this limitation and expresses a desire to eventually remove it (first bullet under "Initial Limitations"). – Upas