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

标签 rust traits

我想对 rusqlite 使用准备好的语句. Rusqlite 为 String&stra bunch of other types 实现了特征 ToSql :

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 对象 是一种不同的类型,并且通常与底层类型的大小不同;将值打包到向量中时,两者都是重要的考虑因素。

另一个问题是您不能创建一个 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!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 )

关于rust - 无法调用 rusqlite 的查询,因为它需要类型 &[&rusqlite::types::ToSql],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624591/

相关文章:

rust - 在 Rust 中,如何将发散函数作为参数传递给另一个函数

rust - 默认泛型参数

generics - Rust 中相互依赖的通用特征

php - 不确定 Laravel 在接口(interface)实现方面做了什么

rust - 如何在 `cargo doc` 生成的文档中获取功能需求标签?

rust - 将切片从静态回调转发到 channel 时存在冲突的生命周期要求

generics - 在Rust中编写一个将可迭代容器作为参数的泛型函数

scala 扩展特征,方法返回各种类型,类型不匹配

class - 在 Scala 中,将对象方法组合为类方法

rust - 不安全的赋值返回空结构