Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot infer type for `U`

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: &currency,
            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`
like image 348
BalaB Avatar asked Sep 16 '17 15:09

BalaB


1 Answers

I strongly recommend that you go back and re-read The Rust Programming Language, specifically the chapter on generics.


LoadDsl::get_result is defined as:

fn get_result<U>(self, conn: &Conn) -> QueryResult<U> 
where
    Self: LoadQuery<Conn, U>, 

In words, that means that the result of calling get_result will be a QueryResult parameterized by a type of the callers choice; the generic parameter U.

Your call of get_result in no way specifies the concrete type of U. 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:

foo.get_result::<SomeType>(conn)
//            ^^^^^^^^^^^^ 

Or you can save the result to a variable with a specified type:

let bar: QueryResult<SomeType> = foo.get_result(conn);

If you review the Diesel tutorial, you will see a function like this (which I've edited to remove non-relevant detail):

pub fn create_post() -> Post {
    diesel::insert(&new_post).into(posts::table)
        .get_result(conn)
        .expect("Error saving new post")
}

Here, type inference kicks in because expect removes the QueryResult wrapper and the return value of the function must be a Post. Working backwards, the compiler knows that U must equal Post.

If you check out the documentation for insert you can see that you can call execute if you don't care to get the inserted value back:

diesel::insert(&new_user)
    .into(users)
    .execute(&connection)
    .unwrap();
like image 51
Shepmaster Avatar answered Sep 21 '22 15:09

Shepmaster