0

Im using sqlx and im creating a DB struct that will have functions for access the database. Im using the sqlx::query! macro and in the documentation there it says:

The output type is an anonymous record

so my wrapper function needs to return that too. Only I cant find how to do it. In Go you have the interface{} object which is the base object of all objects and then you can cast that. How do I approach this in Rust?

I tried using dyn Any, without really knowing what im doing there, and it says "doesnt have size known at compile time", which it wont. So is there a way to return a list of any object of unknown size?

Herohtar
  • 5,347
  • 4
  • 31
  • 41
discodowney
  • 1,475
  • 6
  • 28
  • 58
  • You'll want to use [`query_as!`](https://docs.rs/sqlx/0.5.11/sqlx/macro.query_as.html), which specifically states: *"**This lets you return the struct from a function** or add your own trait implementations."* – Herohtar Apr 05 '22 at 16:02
  • ran into problems using that and just looking at the alternatives while i wait to hear back about a question on that – discodowney Apr 05 '22 at 16:05
  • `Box` would technically let you return the value, but the caller wouldn't be able to do anything with it unless they know its type (or a trait it implements that would be useful). – cdhowie Apr 05 '22 at 16:08
  • 2
    What problems did you run into with `query_as!`? Post your errors or whatever. – PitaJ Apr 05 '22 at 17:39
  • You don't appear to have a question about `query_as!`. Your [other related question](https://stackoverflow.com/questions/71753741/select-query-returns-no-data) is also using `query!`. You should post an actual question about the issues you had there. – Herohtar Apr 05 '22 at 18:14
  • sorry, I posted it on the git hub of the crate. I was in the middle of writing it here but made a bit of a breakthrough. I can get the row data, im just trying to see now if i can get the fields inside that row now – discodowney Apr 05 '22 at 18:15

0 Answers0