我试图得到一个Vec<Option<String>>,这样我就可以把它传递给sql,而null值不变.基于this SO question,我试过:

let names: Vec<Option<String>> = merged_df
    .column("description")?
    .str()?
    .into_iter()
    .map(|s| s.to_string())
    .collect();

但这是失败的:

 1  error[E0599]: the method `to_string` exists for enum `Option<&str>`, but its trait bounds were not satisfied
     --> rust/load_all.rs:569:20
      |
 569  |         .map(|s| s.to_string())
      |                    ^^^^^^^^^ method cannot be called on `Option<&str>` due to unsatisfied trait bounds
      |
     ::: /Users/nick/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:570:1
      |
 570  | pub enum Option<T> {
      | ------------------ doesn't satisfy `_: TemporalMethods`, `std::option::Option<&str>: AsSeries`, `std::option::Option<&str>: ToString
 ` or `std::option::Option<&str>: std::fmt::Display`
      |
      = note: the following trait bounds were not satisfied:
              `std::option::Option<&str>: AsSeries`
              which is required by `std::option::Option<&str>: polars::prelude::TemporalMethods`
              `std::option::Option<&str>: std::fmt::Display`
              which is required by `std::option::Option<&str>: ToString`

我怎么才能让它工作?

推荐答案

你少了map()分.你需要两个:一个用于迭代器,另一个用于Option:

let names: Vec<Option<String>> = merged_df
    .column("description")?
    .str()?
    .into_iter()
    .map(|s| s.map(|s| s.to_string()))
    .collect();

附注,您应该更喜欢使用本地Polars操作而不是像这样的转换.这样会更有效率.

Rust相关问答推荐

在‘await’点上使用‘std::同步::Mutex’是否总是会导致僵局?

为什么迭代器上的`. map(...)`的返回类型如此复杂?

常量泛型和类型枚举箱有重叠的用途吗?

函数内模块的父作用域的访问类型

告诉Rust编译器返回值不包含构造函数中提供的引用

无符号整数的Rust带符号差

将一个泛型类型转换为另一个泛型类型

如何初始化选项<;T>;数组Rust 了?

这是什么:`impl Trait for T {}`?

确保参数是编译时定义的字符串文字

Rust Redis 中的 HSET 命令问题

如何限制通用 const 参数中允许的值?

为什么 for_each 在释放模式(cargo run -r)下比 for 循环快得多?

如何展平以下嵌套的 if let 和 if 语句?

返回引用字符串的future

有没有办法在 Rust 中对 BigInt 进行正确的位移?

为什么我不能将元素写入 Rust 数组中移动的位置,但我可以在元组中完成

Rust:为什么在 struct 中borrow 引用会borrow 整个 struct?

为什么 `ref` 会导致此示例*取消引用*一个字段?

为什么 Bevy 的 Trait 边界不满足 Rapier 物理插件?