extern crate serde_json; use serde_json::value; use std::fs::file; use std::io::prelude::*; fn main() { let filepath = "./map/test/anhui.txt"; match file::open(filepath) { err(why) => println!("open file failed : {:?}", why.kind()), ok(mut file) => { let mut content: string = string::new(); file.read_to_string(&mut content); println!("{}", &mut content); serde_json::from_str(&mut content); } } }
error info:
error: unable infer enough type information `_`; type annotations or generic parameter binding required [--explain e0282] --> src/main.rs:16:17 |> 16 |> serde_json::from_str(&mut content); |> ^^^^^^^^^^^^^^^^^^^^
to fix it, need tell compiler type expecting result of serde_json::from_str
. can change line
serde_json::from_str(&mut content);
to
serde_json::from_str::<value>(&mut content);
the reason need specify type becasue serde_json::from_str
generic function needs type instantiated concrete function. rustc takes care of it, , infer type want use, in case, there not enough information let compiler infer because type referred in result of function while result never used in given code.
you may want use result of from_str
expression, otherwise function call nothing. if specify type when using let binding, compiler able infer type, this:
let result: value = serde_json::from_str(&mut content);
Comments
Post a Comment