Does type inference favor return type over arguments?

⚓ Rust    📅 2025-07-09    👤 surdeus    👁️ 3      

surdeus

I had some code like this:

let rc1: Rc<Foo> = ...
let rc2: Rc<dyn Tr> = Rc::clone(&rc1); // Error

It didn't compile because it inferred that as:

let rc2: Rc<dyn Tr> = Rc::<dyn Tr>::clone(&rc1);

And then could not type coerce the argument &Rc<Foo> to &Rc<dyn Tr>. But if I wrote it explicitly, it compiled:

let rc2: Rc<dyn Tr> = Rc::<Foo>::clone(&rc1);

Because now it can coerce the return, Rc<Foo> to Rc<dyn Tr>.

That leads to my question... are there an rules about what is favored in type inference? If it had favored the argument there, it could have inferred clone as being clone(&Rc<Foo>) instead of clone(Rc<dyn Tr>).

1 post - 1 participant

Read full topic

🏷️ rust_feed