Multiple `new` found


#1

So I’ve got a custom type.

pub struct MyType<T> {
  //...
}

And it works when I’ve implemented just one new.

impl MyType<char> {
  pub fn new(chars: Vec<char>) -> MyType<char> {
  }
}

And my test calls it with:

let base2 = MyType::new(vec!['0','1']);

But once I implement a new for another type everything fails.

impl MyType<String> {
  pub fn new(chars: Vec<String>) -> MyType<String> {
  }
}

And the errors all complain about too many new methods.

error[E0034]: multiple applicable items in scope
 --> tests/lib.rs:6:15
  |
6 |   let base2 = MyType::new(vec!['0','1']);
  |               ^^^^^^^^^^^^^^^ multiple `new` found
  |
  = note: candidate #1 is defined in an impl for the type `my_type::MyType<char>`
  = note: candidate #2 is defined in an impl for the type `my_type::MyType<std::string::String>`

But if I try changing new to pub fn new<char> and pub fn new<String> all the traits that used to work on char are now showing as missing.

Can I have Rust simply infer which new method I mean by the kind of parameter I give it?


#3

You can explicitly specify what you mean like MyType::<char>::new().