-
Couldn't load subscription status.
- Fork 13.9k
Description
If the method explicitly expects a trait object, such as &postgres::types::ToSql, (ToSql is a trait), and the user is trying to pass it a type that implements the trait, such as String, it's likely that the user either doesn't understand the distinction between trait object and trait bounds in generics, or has accidentally missed that the function isn't expecting a generic type but a trait object.
In this case, it would be helpful to show a more specific error message than the standard "type mismatch". The standard error message is like this:
error[E0308]: mismatched types
--> src/main.rs:75:39
|
75 | for row in &conn.query(get_table, schemas.as_slice()).unwrap() {
| ^^^^^^^^^^^^^^^^^^ expected trait postgres::types::ToSql, found struct `std::string::String`
|
= note: expected type `&[&postgres::types::ToSql]`
found type `&[&std::string::String]`
First of all, "expected trait" is downright misleading, I think it should be "expected trait object". Secondly, it would be helpful to show a hint that says that:
Stringimplements traitToSql, but this method is expecting a trait object, not any type that implements the trait.- You can create a trait object from a value of type
&Stringwithvalue as &postgres::types::ToSql.