首页 > 解决方案 > ? 操作员生锈导致结果类型出错

问题描述

生锈的新手。我正在尝试使用?运算符转换 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);
    }
}

标签: rust

解决方案


推荐阅读