Turbofish无法正确推断类型

时间:2020-04-04 18:38:54

标签: mysql rust

我正在使用mysql条板箱,尤其是query_first method。运行下面的代码段可以很好地进行编译。

let foo : mysql::Result<Option<u64>> = tx.query_first("SELECT row_count();");

出于人体工程学的目的,我希望将该调用嵌入match的顶层,以便可以测试多种情况。为了推断出缺失的类型,我使用了turbofish运算符(请参见以下代码段)。但是,这不能编译。

match tx.query_first::<mysql::Result<Option<u64>>>("SELECT row_count();") {
  Ok(Some(num)) => {}
  Ok(None) => {}
  Err(e) => {}
};

我不正确使用turbofish吗?

编译器错误为:

error[E0107]: wrong number of type arguments: expected 2, found 1
   --> src/main.rs:224:22
    |
224 |             match tx.query_first::<mysql::Result<Option<u64>>>("SELECT row_count();") {
    |                      ^^^^^^^^^^^ expected 2 type arguments

error[E0277]: the trait bound `std::result::Result<std::option::Option<u64>, mysql::error::Error>: mysql_common::value::convert::FromValue` is not satisfied
   --> src/main.rs:224:22
    |
224 |             match tx.query_first::<mysql::Result<Option<u64>>>("SELECT row_count();") {
    |                      ^^^^^^^^^^^ the trait `mysql_common::value::convert::FromValue` is not implemented for `std::result::Result<std::option::Option<u64>, mysql::er
ror::Error>`
    |
    = note: required because of the requirements on the impl of `mysql_common::row::convert::FromRow` for `std::result::Result<std::option::Option<u64>, mysql::error::Err
or>`

1 个答案:

答案 0 :(得分:4)

由于函数query_first具有两个通用参数,因此您仍然必须在turbofish表示法中提供两个参数。

另一个问题是您传递给turbofish的类型错误:query_first返回Result<Option<T>>,因此,如果要获取Result<Option<u64>>,则T必须为{{1} }。

将其与第二种类型的忽略绑定u64结合在一起,使编译器可以推断出它,因此这应该起作用(因为通用类型Q是第二个参数):

_