The following code results in two compiler errors:
use serde::Deserialize;
use serde_json::json;
#[derive(Deserialize, Eq, PartialEq)]
struct Foo {
bar: u32,
}
fn main() {
let foo: Foo = serde_json::from_value(json!({"bar": 42})).unwrap();
let glonk = Foo { bar: 42 };
assert_eq!(foo, glonk);
}
The first error is "Foo doesn't implement Debug" on the last line, which is easy to understand – assert_eq!() requires Debug. The other error is quite weird, though:
error[E0283]: type annotations needed
--> src/main.rs:10:43
|
10 | let foo: Foo = serde_json::from_value(json!({"bar": 42})).unwrap();
| ^^^^^^^^^^^^^^^^^^ cannot infer type for type `{integer}`
|
= note: multiple `impl`s satisfying `{integer}: Serialize` found in the `serde` crate:
- impl Serialize for i128;
- impl Serialize for i16;
- impl Serialize for i32;
- impl Serialize for i64;
and 8 more
= note: required because of the requirements on the impl of `Serialize` for `&{integer}`
This error disappears once I add a Debug implementation for Foo, or once I remove the assert_eq!(), but it's still confusing. Why is anything requiring Serialize here? And why has the compiler suddenly trouble inferring the type of the integer? Annotating the integer as 42u32 also fixed the error.