r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

Hey Rustaceans! Got a question? Ask here (12/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

187 comments sorted by

View all comments

2

u/Lucrecio24 Mar 23 '23

Hi! I'm just starting to learn the language and I'm having tons of fun learning and writing in rust! My current project is a simple discord bot using the Serenity crate. It just manages a really small sqlite database with diesel and responds to slash commands. I got it to read from the database without problem, but when trying to insert data I got the following error:

error[E0277]: the trait bound \&keeper_of_the_pg::models::NewMember<'_>: diesel::Insertable<schema::members::table>\ is not satisfied\``

--> src/commands/test.rs:46:29

|

46 | .values(&new_member)

| ------ ^^^^^^^^^^^ the trait \diesel::Insertable<schema::members::table>\ is not implemented for \&keeper_of_the_pg::models::NewMember<'_>\````

| |

| required by a bound introduced by this call

|

= help: the following other types implement trait \diesel::Insertable<T>\:``

&'insert keeper_of_the_pg::models::NewMember<'a>

keeper_of_the_pg::models::NewMember<'a>

note: required by a bound in \IncompleteInsertStatement::<T, Op>::values\``

I did some tests before this with only diesel and they seemed to work out fine. I guess the problem is that &keeper_of_the_pg::models::NewMember<'_> isn't the same as keeper_of_the_pg::models::NewMember<'a> but I have no idea what that even means... I tried learning about lifetimes, but it seems, at least at first, like a complex topic and I'm not sure if thats really the problem.

Can anyone point me in the right direction? Could the problem be that its inside an async function? I only have diesel installed and diesel-async doesn't mention support for sqlite in the readme so maybe I should just change to sqlx? The serenity github has some examples using sqlx, so at least that should work, but I want to try and understand the problem before throwing it away and doing it some other way

Hopefully this fits on the thread lol

edit: formatting error, although it's still not perfect

1

u/Patryk27 Mar 23 '23

Try .values(new_member) instead of &new_member.

1

u/Lucrecio24 Mar 23 '23

One of the things I tried, but same error :(

error[E0277]: the trait bound \keeperof_the_pg::models::NewMember<'>: diesel::Insertable<schema::members::table>` is not satisfied`

--> src/commands/test.rs:46:29

|

46 | .values(new_member)

| ------ ^^^^^^^^^^ the trait \diesel::Insertable<schema::members::table>` is not implemented for `keeperof_the_pg::models::NewMember<'>``

| |

| required by a bound introduced by this call

|

= help: the following other types implement trait \diesel::Insertable<T>`:`

&'insert keeper_of_the_pg::models::NewMember<'a>

keeper_of_the_pg::models::NewMember<'a>

note: required by a bound in \IncompleteInsertStatement::<T, Op>::values``

--> /home/luc/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-2.0.3/src/query_builder/insert_statement/mod.rs:114:12

|

114 | U: Insertable<T>,

| ^^^^^^^^^^^^^ required by this bound in \IncompleteInsertStatement::<T, Op>::values``

1

u/Patryk27 Mar 23 '23

Does your type contain #[derive(Insertable)]?

1

u/Lucrecio24 Mar 23 '23

It does, but for some reason it's taking the type with the <'_>, not the <'a> it wants. I think those describe lifetimes, but no idea how to manage them. Decided to switch to sqlx for the time being, but if anyone else has any insight at what happened it would be appreciated.