{"id":1214,"hash":"5a51299a86b2b03cda05e7d66e27aad9e0a4502cb9206df6a9d5b55823700a9c","pattern":"Cannot infer type for `U`","full_message":"I am using Rust and Diesel:\n\nfn create_asset_from_object(assets: &HashMap<String, Assets_Json>) {\n    let connection: PgConnection  = establish_connection();\n    println!(\"==========================================================\");\n    insert_Asset(&connection, &assets);\n}\n\npub fn insert_Asset(conn: &PgConnection, assests: &HashMap<String, Assets_Json>){\n    use self::schema::assets;\n\n    for (currency, assetInfo) in assests {\n\n        let new_asset = self::models::NewAssets {\n            asset_name: &currency,\n            aclass:  &assetInfo.aclass,\n            altname: &assetInfo.altname,\n            decimals:  assetInfo.decimals,\n            display_decimals: assetInfo.display_decimals,\n        };\n\n       //let result = diesel::insert(&new_asset).into(assets::table).get_result(conn).expect(\"Error saving new post\");\n       println!(\"result, {:#?}\", diesel::insert(&new_asset).into(assets::table).get_result(conn).expect(\"Error saving new post\"));\n\n    }\n}\n\nCompiler error:\n\nerror[E0282]: type annotations needed\n   --> src/persistence_service.rs:107:81\n    |\n107 |        println!(\"result, {:#?}\", diesel::insert(&new_asset).into(assets::table).get_result(conn).expect(\"Error saving new post\"));\n    |                                                                                 ^^^^^^^^^^ cannot infer type for `U`","ecosystem":"cargo","package_name":"rust-diesel","package_version":null,"solution":"I strongly recommend that you go back and re-read The Rust Programming Language, specifically the chapter on generics.\n\nLoadDsl::get_result is defined as:\n\nfn get_result<U>(self, conn: &Conn) -> QueryResult<U> \nwhere\n    Self: LoadQuery<Conn, U>, \n\nIn 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. \n\nYour 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.\n\nYou can use the turbofish operator:\n\nfoo.get_result::<SomeType>(conn)\n//            ^^^^^^^^^^^^ \n\nOr you can save the result to a variable with a specified type:\n\nlet bar: QueryResult<SomeType> = foo.get_result(conn);\n\nIf you review the Diesel tutorial, you will see a function like this (which I've edited to remove non-relevant detail):\n\npub fn create_post() -> Post {\n    diesel::insert(&new_post).into(posts::table)\n        .get_result(conn)\n        .expect(\"Error saving new post\")\n}\n\nHere, 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.\n\nIf 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:\n\ndiesel::insert(&new_user)\n    .into(users)\n    .execute(&connection)\n    .unwrap();","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/46255634/cannot-infer-type-for-u","votes":12,"created_at":"2026-04-19T04:52:37.214074+00:00","updated_at":"2026-04-19T04:52:37.214074+00:00"}