从文件中读取行,迭代每行和该行中的每个字符

时间:2015-06-11 20:19:36

标签: loops character line rust contains

我需要读取一个文件,获取每一行,遍历每一行并检查该行是否包含“aeiuo”中的任何字符,如果它包含至少2个字符“äüö”。

这段代码是惯用的Rust吗?如何检查String中的多个字符?

到目前为止,我的一些谷歌和代码窃取的尝试:

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("foo.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();

    for l in lines {
        if (l.unwrap().contains("a")) {
            println!("here is a");
        }
    }
}

(Playground link)

2 个答案:

答案 0 :(得分:3)

1)&#34;这段代码是惯用的Rust吗?&#34;

总的来说,这似乎很好。您可能希望改进一个小问题:您不需要将线条收集到矢量中以迭代它们。这是不需要的,因为它会触发不需要的内存分配。只需直接读取lines()迭代器即可。 (如果你来自C ++,你可以忘记将事物收集到中间向量中:思考功能,思考迭代器!)

let reader = BufReader::new(file);
let lines: Vec<_> = reader.lines().collect();

for l in lines {
    ...
}

变为

let reader = BufReader::new(file);
let lines = reader.lines(); 
// lines is a instance of some type which implements Iterator<Item=&str>

for l in lines {
    ...
}

2)&#34;如何检查字符串中的多个字符?&#34;

我建议采用基于.any()的简单方法:

fn is_aeiou(x: &char) -> bool {
    "aeiou".chars().any(|y| y == *x)
}

fn is_weird_auo(x: &char) -> bool {
    "äüö".chars().any(|y| y == *x)
}

fn valid(line: &str) -> bool {
    line.chars().any(|c| is_aeiou(&c)) &&
    line.chars().filter(is_weird_auo).fuse().nth(1).is_some()
}

然后你可以一直使用迭代器并按如下方式编写主要测试:

let reader = BufReader::new(file);
let lines = reader.lines();

let bad_line = lines.map(|l| l.unwrap()).filter(|line| !valid(line)).next();
match bad_line {
    Some(line_n) => println!("Line {} doesn't pass the test", line_n),
    None => println!("All lines are good!"),
}

// Alternate way if you don't need the line number. More readable
//let all_good = lines.map(|l| l.unwrap()).all(valid);

playground上的完整代码。)

答案 1 :(得分:1)

这有效,thanks to the nice people on /r/rust

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

fn is_vowel(x: &char) -> bool {
    "aAeEiIoOuU".chars().any(|y| y == *x)
}

fn is_umlaut(x: &char) -> bool {
    "äÄüÜöÖ".chars().any(|y| y == *x)
}

fn valid(line: &str) -> bool {
    line.chars().all(|c| !is_vowel(&c)) && line.chars().filter(is_umlaut).fuse().nth(1).is_some()
}

fn main() {
    // Create a path to the desired file
    let path = Path::new("c.txt");
    let display = path.display();
    // Open the path in read-only mode, returns `io::Result<File>`
    let file = match File::open(&path) {
        Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };
    let reader = BufReader::new(file);
    for line in reader.lines() {
        match line {
            Ok(line) => {
                if valid(&line) {
                    println!("{}", line)
                }
            }
            Err(e) => println!("ERROR: {}", e),
        }
    }
}