Assuming the following Rust code:
trait MyTrait {...}
struct MyStruct;
impl MyTrait for MyStruct {...}
impl MyStruct {
pub fn new() -> MyStruct { MyStruct{...} }
}
I'm able to write:
let foo: MyStruct = MyStruct::new();
However I can't
let bar: MyTrait = MyStruct::new();
Other interface-oriented programming languages can do that. Am I missing something? How can I pass various objects to a method that accepts only MyTrait
? Do I need to use "templates" such as my_method<T: MyTrait>(param: T) {...}
?