I have a MySQL database and I want to make the simplest possible query in rust to verify that I'm connecting to the database and able to retrieve data.
use diesel::prelude::*;
use diesel::mysql::MysqlConnection;
use diesel::dsl::sql_query;
use dotenvy::dotenv;
use std::env;
fn establish_connection() -> MysqlConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
MysqlConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
fn print_db_properties() {
let connection = &mut establish_connection();
let results = sql_query("SELECT version()")
.load(connection)
.expect("error");
}
fn main() {
print_db_properties();
}
I get the error:
error[E0283]: type annotations needed for `Vec<U>`
--> src/main.rs:32:9
|
32 | let results = query
| ^^^^^^^
33 | .load(connection)
| ---- ---------- type must be known at this point
| |
| required by a bound introduced by this call
|
= note: cannot satisfy `_: FromSqlRow<Untyped, Mysql>`
= help: the following types implement trait `FromSqlRow<ST, DB>`:
<(T1, T0) as FromSqlRow<(ST1, Untyped), __DB>>
<(T1, T2, T0) as FromSqlRow<(ST1, ST2, Untyped), __DB>>
<(T1, T2, T3, T0) as FromSqlRow<(ST1, ST2, ST3, Untyped), __DB>>
<(T1, T2, T3, T4, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T6, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, ST6, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T6, T7, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, ST6, ST7, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T6, T7, T8, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, Untyped), __DB>>
and 23 others
= note: required for `Untyped` to implement `load_dsl::private::CompatibleType<_, Mysql>`
= note: required for `SqlQuery` to implement `LoadQuery<'_, MysqlConnection, _>`
note: required by a bound in `diesel::RunQueryDsl::load`
--> /home/bschwyn/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.1.4/src/query_dsl/mod.rs:1543:15
|
1541 | fn load<'query, U>(self, conn: &mut Conn) -> QueryResult<Vec<U>>
| ---- required by a bound in this associated function
1542 | where
1543 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
help: consider giving `results` an explicit type, where the type for type parameter `U` is specified
|
32 | let results: Vec<U> = query
| ++++++++
For more information about this error, try `rustc --explain E0283`.
The error is caused by a generic type not being known and could be resolved by something like:
let results: Vec<U> = query...
or
let results = query.load::<U>(connection).expect("error");
My question is how do I determine what type "U" will be?
More info:
Load is going to return a type QueryResult<Vec<U>>
as per the docs. It has the definition:
fn load<U>(self, conn: &Conn) -> QueryResult<Vec<U>>
where
Self: LoadQuery<Conn, U>,
In the docs there is no type referenced by "let users =..." so I'm not sure what they did differently.
Rust does not have type introspection: , but I was able to write a print_type_of method to get the type of sql_query("...") as diesel::query_builder::sql_query::SqlQuery.
Other examples such as this one are for queries of columns/rows and such with defined structs. Do I need something like that?