I am using Rust and Diesel:
fn create_asset_from_object(assets: &HashMap<String, Assets_Json>) {
let connection: PgConnection = establish_connection();
println!("==========================================================");
insert_Asset(&connection, &assets);
}
pub fn insert_Asset(conn: &PgConnection, assests: &HashMap<String, Assets_Json>){
use self::schema::assets;
for (currency, assetInfo) in assests {
let new_asset = self::models::NewAssets {
asset_name: ¤cy,
aclass: &assetInfo.aclass,
altname: &assetInfo.altname,
decimals: assetInfo.decimals,
display_decimals: assetInfo.display_decimals,
};
//let result = diesel::insert(&new_asset).into(assets::table).get_result(conn).expect("Error saving new post");
println!("result, {:#?}", diesel::insert(&new_asset).into(assets::table).get_result(conn).expect("Error saving new post"));
}
}
Compiler error:
error[E0282]: type annotations needed
--> src/persistence_service.rs:107:81
|
107 | println!("result, {:#?}", diesel::insert(&new_asset).into(assets::table).get_result(conn).expect("Error saving new post"));
| ^^^^^^^^^^ cannot infer type for `U`
I strongly recommend that you go back and re-read The Rust Programming Language, specifically the chapter on generics.
LoadDsl::get_resultis defined as:In words, that means that the result of calling
get_resultwill be aQueryResultparameterized by a type of the callers choice; the generic parameterU.Your call of
get_resultin no way specifies the concrete type ofU. In many cases, type inference is used to know what the type should be, but you are just printing the value. This means it could be any type that implements the trait and is printable, which isn't enough to conclusively decide.You can use the turbofish operator:
Or you can save the result to a variable with a specified type:
If you review the Diesel tutorial, you will see a function like this (which I've edited to remove non-relevant detail):
Here, type inference kicks in because
expectremoves theQueryResultwrapper and the return value of the function must be aPost. Working backwards, the compiler knows thatUmust equalPost.If you check out the documentation for
insertyou can see that you can callexecuteif you don't care to get the inserted value back: