New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diesel error type on Sql_Count #1876

Closed
NachoGotaki opened this Issue Oct 5, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@NachoGotaki

NachoGotaki commented Oct 5, 2018

  • Rust: cargo 1.31.0-nightly
  • Diesel: 1.3.0
  • Database: Mysql
  • Operating System Ubuntu 18
  • diesel:

Hello, first of all i have to say that im totally new in rust/rocket/diesel. Completely new.
Im only trying to print the total of rows in a query. Im doing this.

let total_results = places::table.count().load(connection); println!("{:?}", total_results);

Im trying to get the total to do a paginator, but compilation fails due to this:

error[E0282]: type annotations needed
--> src/v1/places/mod.rs:189:47
|
189 | let total_results = places::table.count().load(connection);
| ------------- ^^^^ cannot infer type for U
| |
| consider giving total_results a type

And in examples i see on the web they never give a type to the variable.
https://docs.diesel.rs/diesel/query_dsl/trait.QueryDsl.html#method.count

Thank in advance

@weiznich

This comment has been minimized.

Contributor

weiznich commented Oct 5, 2018

As far as I can see from that minimal code snippet, the problem seems to be that you use the result only in the print statement. Therefore rustc could not infer the concrete type of total_results because I only knows that those type has to implement a bunch of traits, but multiple types could fulfil that.
Therefore a explicit type annotation is needed here. (In the examples this is not the case because the assert_eq! explicitly puts a value (with a type) to compare in there.)

To make this work there are two solutions:

  • Add a type annotation to the variable: let total_results: Result<Vec<i64>, _> = places::table.count().load(connection);
  • Add the generic type parameter explicitly to the method call: let total_results = places::table.count().load::<i64>(connection);

(Closed this issue because our issue tracker should only contain actionable open issues. Feel free to ask such questions in our gitter channel or our forum

@weiznich weiznich closed this Oct 5, 2018

@NachoGotaki

This comment has been minimized.

NachoGotaki commented Oct 5, 2018

thank you too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment