Read lines from file, iterate over each line and each character in that line

前端 未结 2 1653
小鲜肉
小鲜肉 2021-01-21 03:20

I need to read a file, get each line, iterate over each line and check if that line contains any character from "aeiuo" and if it contains at least 2 of the characters

相关标签:
2条回答
  • 2021-01-21 03:28

    This works, 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),
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-21 03:43

    1) "Is this code idiomatic Rust?"

    Overall yes, it seems good. There is one minor point that you probably want to improve: you don't need to collect the lines into a vector to iterate on them. This is unwanted because it triggers unneeded memory allocations. Just reading the lines() iterator directly will work. (If you come from C++, you can forget about collecting things into intermediary vectors: think functional, think iterators!)

    let reader = BufReader::new(file);
    let lines: Vec<_> = reader.lines().collect();
    
    for l in lines {
        ...
    }
    

    becomes

    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) "How do I check for several characters in a string?"

    I suggest a simple approach based on .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()
    }
    

    Then you can go iterators all the way and write your main test as follows:

    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);
    

    (Full code on the playground.)

    0 讨论(0)
提交回复
热议问题