Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Does type inference favor return type over arguments?
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
🏷️ rust_feed