I\'m trying to find examples for constructor functions in traits, but haven\'t had much luck. Is this a idiomatic thing to do in Rust?
You need to use the Self
type. In trait declarations, Self
refers to the type that implements a trait. In your case, the trait declaration should look as follows:
trait A {
fn new() -> Self; // Self stands for any type implementing A
}
Your original version is subtly different because it will return a trait object, not a value of the implementor type.