无法推断“U"的类型 [英] Cannot infer type for `U`

查看:101
本文介绍了无法推断“U"的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rust 和 Diesel:

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"));

    }
}

编译器错误:

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`

推荐答案

我强烈建议你回去重新阅读Rust 编程语言,特别是 泛型章节.

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

LoadDsl::get_result 定义为:

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

换句话说,这意味着调用 get_result 的结果将是一个由 callers 选择的类型参数化的 QueryResult;通用参数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.

您对get_result 的调用绝不指定U 的具体类型.在许多情况下,类型推断用于知道类型应该是什么,但您只是打印值.这意味着它可以是任何类型,它实现了 trait 并且是可打印的,这还不足以最终决定.

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.

您可以使用 turbofish 运算符:

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")
}

在这里,类型推断开始起作用,因为 expect 删除了 QueryResult 包装器,并且函数的返回值必须是 Post.向后工作,编译器知道 U 必须等于 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.

如果您查看文档insert 你可以看到你可以调用 execute 如果你不关心获取插入的值:

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();

这篇关于无法推断“U"的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆