Rust Polars在Cargo.toml特性选择失效及版本强制问题
Polars版本强制指定与特性失效问题解决
问题现象
- 指定Polars 0.22.8版本并开启
lazy特性时,代码可正常使用LazyCsvReader - 指定0.23.0版本时,Cargo自动拉取0.23.2版本,代码报错无法识别
LazyCsvReader类型 - 0.23.1版本特性树完整无此问题,但直接指定0.23.1版本未生效
相关配置与代码
Cargo.toml(原配置)
[package] name = "test" version = "0.1.0" edition = "2021" [dependencies] # Version 0.22.7 ==> works polars = {version = "0.22.8", features = ["lazy"]} # Version 0.23.0 ==> Does Not Work ... and it will load the 0.23.2 version?! #polars = {version = "0.23.0", features = ["lazy"]}
main.rs
use polars::prelude::*; pub fn main() { let path = "C:\\temp\\rusty.csv"; let days = LazyCsvReader::new(path.into()) .has_header(false) .finish() .unwrap() .collect(); }
报错信息
error[E0433]: failed to resolve: use of undeclared type `LazyCsvReader` --> src\main.rs:25:16 | 25 | let days = LazyCsvReader::new(path.into()) | ^^^^^^^^^^^^^ use of undeclared type `LazyCsvReader`
0.22.8版本特性树(正常)
│ ├── polars feature "csv-file" │ │ ├── polars v0.22.8 (*) │ │ ├── polars feature "polars-io" │ │ │ └── polars v0.22.8 (*) │ │ ├── polars feature "polars-lazy" │ │ │ └── polars v0.22.8 (*) │ │ ├── polars-io feature "csv-file" (*) │ │ └── polars-lazy feature "csv-file" │ │ ├── polars-lazy v0.22.7 (*) │ │ └── polars-io feature "csv-file" (*)
问题原因
- Cargo版本默认行为:未加约束的版本号(如
0.23.0)等价于^0.23.0,Cargo会自动拉取同大版本下的最新兼容版本(即0.23.2) - 特性拆分:Polars 0.23.2版本调整了特性树,
lazy特性不再默认包含csv-file相关依赖,导致LazyCsvReader无法被引入 - 版本指定错误:直接写
0.23.1仍会被Cargo解析为^0.23.1,可能因其他依赖冲突或缓存问题未生效
解决方案
方案1:强制使用精确版本0.23.1
修改Cargo.toml中的Polars依赖,使用=前缀指定精确版本,阻止Cargo自动升级:
polars = {version = "=0.23.1", features = ["lazy"]}
方案2:适配0.23.2版本,显式添加缺失特性
如果需要使用0.23.2版本,需显式添加csv-file特性,确保LazyCsvReader被引入:
polars = {version = "0.23.2", features = ["lazy", "csv-file"]}
验证步骤
- 清理Cargo缓存:执行
cargo clean - 重新构建项目:执行
cargo build - 检查依赖版本:执行
cargo tree | grep polars确认版本符合预期
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

