Can a function define constructor / initializer methods?

The method defined by the attribute can take the implementation of the structure as a parameter using a keyword self. But I cannot figure out how to return an instance of the implementing structure. For instance:

trait Construct {
    fn use_self(self) -> uint;
    fn make_self(n: uint) -> self;
}

use_selfexcellent and compiles, but make_selfdoes not compile. Is there a way for the trait method to return an instance of the implementing structure, such as a constructor or initializer?

+4
source share
1 answer

Is there a way for the trait method to return an instance of the implementing structure, such as a constructor or initializer?

Yes. Use Selfas return type when declaring a tag method.

+5
source

All Articles