rust - ? 操作员生锈导致结果类型出错
问题描述
生锈的新手。我正在尝试使用?
运算符转换 Result 类型。注释掉的代码有效,但是用使用运算符的行替换它会产生此错误:
^^ cannot use the `?` operator in a function that returns `()`
为什么是这样?我想使用 ? 在处理 Result 类型时应该始终有效,但大多数时候我尝试它坚持返回 (),即使匹配语句表明它返回 Result 类型。我在文档中看到过这种类型的语法,它说它应该没问题。
pub fn new(csv_file: String, portfolio_key: String, volatility_divisor: f64, raw_threshold: f64) {
let file = File::open(&csv_file[..]).expect("Bad file name");
let mut reader = csv::Reader::from_reader(file);
for l in reader.records() {
// let l = match l {
// Ok(file) => file,
// Err(error) => panic!("ERROR: {}", error),
// };
let l = l?;
println!("{:?}", l);
}
}
解决方案
推荐阅读
- android - 逐项滚动卡片列表
- python - 训练测试拆分后丢弃值
- highcharts - 在 X 轴下显示信息,例如在 Excel 中使用 Highcharts
- sql - MS ACESS 按多个标准对列进行排名
- c - 在 C 中查找素数
- python - 如何使用列表理解遍历对象属性?
- vhdl - 用于读取十六进制并转换为 std_logic_vector 的 Vhdl 代码
- openshift - 部署“分蘖”超过了进度期限
- node.js - 将 jest 与 mongoose 一起使用时,“ReferenceError: You are trying to `import` a file after Jest environment has been torn down”
- python - python中的元组和列表有什么区别,哪个更有效