3

I want to use a prepared statement with rusqlite. Rusqlite implements the trait ToSql for String, &str and a bunch of other types:

extern crate rusqlite;

use rusqlite::Connection;

fn main() {
    let mut connection = Connection::open("C:\\test_db.db").unwrap();

    let mut cached_statement = connection
        .prepare_cached("SELECT ?, ?, ? FROM test")
        .unwrap();

    let vec_values = vec![
        &"test1".to_string(),
        &"test2".to_string(),
        &"test3".to_string(),
    ];

    let rows = cached_statement.query(vec_values.as_slice()).unwrap();
}

This does not compile with the error:

error[E0308]: mismatched types
  --> src/main.rs:18:39
   |
18 |     let rows = cached_statement.query(vec_values.as_slice()).unwrap();
   |                                       ^^^^^^^^^^^^^^^^^^^^^ expected trait rusqlite::types::ToSql, found struct `std::string::String`
   |
   = note: expected type `&[&rusqlite::types::ToSql]`
              found type `&[&std::string::String]`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Slater Th
  • 33
  • 4

1 Answers1

6

The compiler message isn't lying to you. You have a &[&String] not a &[&ToSql]. A trait object is a different type and often a different size from the underlying type; both are important considerations when packing values into a vector.

Another problem is that you cannot create a String, take a reference to it, then store that in a variable. The String would be deallocated immediately, leaving a dangling reference, so the compiler prevents that.

The easiest thing you can do is to create a new Vec that contains the trait object references:

let vec_values = vec![
    "test1".to_string(),
    "test2".to_string(),
    "test3".to_string(),
];

let query_values: Vec<_> = vec_values.iter().map(|x| x as &dyn ToSql).collect();

let _rows = cached_statement.query(&query_values).unwrap();

(complete example)

Or if you wanted an overly-generic function to perform the conversion:

fn do_the_thing<'a, I, T: 'a>(things: I) -> Vec<&'a dyn ToSql>
where
    I: IntoIterator<Item = &'a T>,
    T: ToSql,
{
    things.into_iter().map(|x| x as &dyn ToSql).collect()
}
let _rows = cached_statement.query(&do_the_thing(&vec_values)).unwrap();

(complete example)

In many cases, you can use the params! or named_params! macro:

let a = "test1".to_string();
let b = "test2".to_string();
let c = "test3".to_string();

let _rows = cached_statement.query(params![a, b, c]).unwrap();

(complete example)

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thank you for the very detailed answer. This does not work because "std::convert::From<&std::string::String>" is not implemented for "rusqlite::types::ToSqlOutput<'_>" I have not tried the generic solution, but since its basically the same code it should result in the same error. ( x as &ToSql is the problem) – Slater Th Oct 08 '17 at 18:51
  • @SlaterTh I don't know what you mean. Both pieces of code that I have posted compile successfully and I've now added links to the entire compilable examples. I'm guessing that one of us has added or removed something without explicitly mentioning it. – Shepmaster Oct 08 '17 at 19:53
  • Your code is indeed correct, I used &String inside the vec.... Thank you for your time. – Slater Th Oct 08 '17 at 20:57