Turbofish does not infer type correctly

Viewed 74

I'm using the mysql crate, specifically the query_first method. Running the following snippet compiled perfectly.

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

For ergonomic purposes, I want to embed that call at the top-level of a match, so that I can test multiple cases. To infer the missing type, I use the turbofish operator (see following snippet). However, that does not compile.

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

Am I not using turbofish correctly?

The compiler errors are:

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 Answers

Since the function query_first takes two generic parameters, you still have to provide two in the turbofish notation.

The other issue is that you are passing to turbofish the wrong type: query_first returns a Result<Option<T>>, so if you want to get a Result<Option<u64>>, T must be u64.

Combine this together with an ignored binding _ to the second type which lets the compiler infer it, so this should work (as the generic type Q is the second parameter):

query_first::<u64, _>
Related