无法调用 rusqlite 的查询,因为它需要类型 &[&rusqlite::types::ToSql]

Cannot call rusqlite's query because it expects the type &[&rusqlite::types::ToSql]

我想使用准备好的语句 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();
}

这没有编译错误:

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]`

编译器消息没有骗你。你有 &[&String] 而不是 &[&ToSql]trait object 是一种不同的类型,通常与底层类型的大小不同;将值打包到向量中时,两者都是重要的考虑因素。

另一个问题是您无法创建 String,引用它,然后将其存储在变量中。 String 会立即被释放,留下悬空引用,因此编译器会阻止这种情况发生。

您可以做的最简单的事情是创建一个包含特征对象引用的新 Vec

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)

或者,如果您想要一个过于通用的函数来执行转换:

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)

很多情况下,可以使用params! or named_params!宏:

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)