Closed
Description
use std::convert::TryInto;
fn main() {
let _: usize = Some(0usize).map(|host: usize| 0usize).unwrap() + 0usize.try_into().unwrap();
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0284]: type annotations needed for `usize`
--> src/main.rs:4:38
|
4 | let _: usize = Some(0usize).map(|host: usize| 0usize).unwrap() + 0usize.try_into().unwrap();
| ^^^^ consider giving this closure parameter a type
|
= note: cannot resolve `<usize as std::ops::Add<_>>::Output == usize`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0284`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
Coder-256 commentedon Feb 17, 2020
That diagnostic is a bit misleading. This works:
Playground
I guess the compiler can't figure out the type of the 2nd addend, and by extension, what type you expect from
try_into()
. I think in this case, I think it's true thatusize
is the only type that satisfiesT
for bothusize: Add<T, Output = usize>
andusize: TryInto<T>
. However, I am not convinced that the compiler should be expected to deduce this, especially because it is possible to implement both of these traits onusize
for some otherT
(due to rust-lang/rfcs#2451). For example:Playground
In other words, I think it is expected that this would not compile, and the problem is the misleading diagnostic.
alecmocatta commentedon May 9, 2020
Closing as this is fixed in latest nightly, I suspect by #71960. Thanks @estebank!