Closed
Description
Consider this code (playground):
pub fn test(input: &str) -> Result<serde_json::Value, String> {
match serde_json::from_str(input).unwrap() {
Ok(v) => Ok(v),
Err(why) => Err(format!("JSON decode failed: {:?}", why)),
}
}
This looks like perfectly reasonable code, but upon further inspection, you can see an .unwrap()
as a leftover from refactoring. However, Rust chooses a quite confusing way of pointing this out:
error[E0282]: type annotations needed
--> src/lib.rs:4:13
|
4 | Err(why) => Err(format!("JSON decode failed: {:?}", why)),
| ^^^ cannot infer type
As far as I am aware, there is no way to solve this problem with just a type annotation.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jonas-schievink commentedon Jul 28, 2019
Using
from_str::<Result<_, ()>>
compilesAuto merge of #65951 - estebank:type-inference-error, r=<try>
Auto merge of #65951 - estebank:type-inference-error, r=nikomatsakis
estebank commentedon Sep 2, 2020
Current output:
estebank commentedon Aug 3, 2023
Current output: