小编Sta*_*Phy的帖子

ArgMatches 的 get_one 无法向下转型 f64

我已经使用clap crate来解析代码中的参数。我的关于定义和解析参数的代码的最小结构如下。

use clap::builder::Command;
use clap::{Arg, ArgMatches};

let matches = Command::new("test")
                .arg(Arg::new("mass")
                    .short('m')
                    .takes_value(true))
                .get_matches()
let mass: f64 = *matches.get_one::<f64>("mass").unwrap();
Run Code Online (Sandbox Code Playgroud)

但我面临一个错误“线程‘main’因‘定义和访问之间不匹配而惊慌mass。无法向下转换为 f64,需要向下转换为 alloc::string::String”

我可以通过使用 parse() 从 String 到 f64 来修复它。

let mass: f64 = *matches.get_one::<String>("mass").unwrap().parse().unwrap();
Run Code Online (Sandbox Code Playgroud)

我想知道为什么只有f64不能被get_one函数解析与boolean或usize的情况不同。

rust clap

9
推荐指数
1
解决办法
2133
查看次数

标签 统计

clap ×1

rust ×1