使用std :: iter :: Iterator映射将文本文件行解析为数字

nee*_*tza 2 io text file rust

我正在尝试读取和解析Rust中的文本文件.每一行都是有符号整数.我能够使用for line in lines迭代来做到这一点,但我无法用一个iter().map(|l| ...)单行程来做.我得到了一个

expected `&core::result::Result<collections::string::String, std::io::error::Error>`,
found `core::result::Result<_, _>`
Run Code Online (Sandbox Code Playgroud)

当我尝试模式匹配Ok(s) => match s.parse()但我无法深入了解我做错了什么.整个例子如下.底部的代码是产生错误的代码.

谁能说出我做错了什么?

use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;

fn main() {
    // Create a path to the desired file
    let path = Path::new("input/numbers.txt");
    let display = path.display();

    // Open the path in read-only mode, returns `io::Result<File>`
    let file = match File::open(&path) {
        // The `description` method of `io::Error` returns a string that describes the error
        Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };

    // Collect all lines into a vector
    let reader = BufReader::new(file);
    let lines: Vec<_> = reader.lines().collect();

    // Works.
    let mut nums = vec![];       
    for l in lines {
        println!("{:?}", l);
        let num = match l {
            Ok(s) => match s.parse() { 
                Ok(i) => i,
                Err(_) => 0
            },
            Err(_) => 0
        };
        nums.push(num);
    }

    // Doesn't work!       
    let nums: Vec<i64> = lines.iter().map(|l| match l {
        Ok(s) => match s.parse() {
            Ok(i) => i,
            Err(_) => 0
        },
        Err(_) => 0
    });
}
Run Code Online (Sandbox Code Playgroud)

She*_*ter 8

让我们看看完整的错误消息,它指出了我们的错误:

<anon>:5:9: 5:14 error: mismatched types:
 expected `&core::result::Result<&str, ()>`,
    found `core::result::Result<_, _>`
(expected &-ptr,
    found enum `core::result::Result`) [E0308]
<anon>:5         Ok(s) => match s.parse() {
                 ^~~~~
Run Code Online (Sandbox Code Playgroud)

编译器期待a &Result,但找到了a Result,问题在于Ok(s)模式.类型l是对a的引用,Result因为您正在使用iter- 它返回对向量中项目的引用的迭代器.

最短的修复是&为闭包变量添加一个模式匹配:

fn main() {
    let lines: Vec<Result<_, ()>> = vec![Ok("1"), Ok("3"), Ok("5")];

    // HERE                                V 
    let nums: Vec<i64> = lines.iter().map(|&l| match l {
        Ok(s) => match s.parse() {
            Ok(i) => i,
            Err(_) => 0
        },
        Err(_) => 0
    }).collect();

    println!("{:?}", nums)
}
Run Code Online (Sandbox Code Playgroud)

我还必须添加collect回去Vec.

您可以进行的另一个更改是使用输入向量into_iter,然后迭代向量中的每个值:

// HERE                    V~~~~
let nums: Vec<i64> = lines.into_iter().map(|l| match l {
Run Code Online (Sandbox Code Playgroud)

为了更好的衡量,您可以使用ok,and_then并且unwrap_or更简洁地说同样的事情:

let nums: Vec<i64> = lines.into_iter().map(|l| {
    l.ok().and_then(|s| s.parse().ok()).unwrap_or(0)
}).collect();
Run Code Online (Sandbox Code Playgroud)