Help me. reading file and extract content

If you want to work on the efficiency of your program, you should make your algorithm more efficient. It does a lot of needless iterations you can avoid.

Given the structure of your input (field name in one line followed by its value in the next line), we can extract all the fields in one single pass over your input, reducing the time complexity from O(lines * log(lines)) to O(lines):

fn main() {
    let mut original_title: &str = "";
    let mut year: &str = "";
    let mut country: &str = "";
    
    
    let reader = "\
Original Title
    Paradise Highway 
Year
    2022
Country
    United States";
    
    let mut lines = reader.lines();
    
    while let (Some(key), Some(value)) = (lines.next(), lines.next()) {
        if key.contains("Original Title") {
            original_title = value;
        } else if key.contains("Year") {
            year = value;
        } else if key.contains("Country") {
            country = value;
        }
    }
    
    println!("Original title is -> {}", original_title);
    println!("Year is -> {}", year);
    println!("Country is -> {}", country);
}

Playground.

2 Likes

Output empty:
Original title is:
Year is:
Country is:
Sinopsis is:

No it isn't. Did you check the linked Playground?

1 Like

Here an example that uses something closer to your input. Your files start with a line containing the special unicode character \u{feff}. We have to skip this line or else our algorithm is off, having keys in the position of values and values in the position of keys when we try and parse the input as lines. So all we got to do is skip reading the first line:

fn main() {
    let mut original_title: &str = "";
    let mut year: &str = "";
    let mut country: &str = "";
    
    
    let reader = "\u{feff}\r\nOriginal Title\r\n Plane\r\nAKA\r\n The Plane\r\nYear\r\n 2022\r\nCountry\r\n United States\r\n";
    
    let mut lines = reader.lines();
    
    // skip first line "\u{feff}"
    lines.next();
    
    while let (Some(key), Some(value)) = (lines.next(), lines.next()) {
        if key.contains("Original Title") {
            original_title = value;
        } else if key.contains("Year") {
            year = value;
        } else if key.contains("Country") {
            country = value;
        }
    }
    
    println!("Original title is -> {}", original_title);
    println!("Year is -> {}", year);
    println!("Country is -> {}", country);
}

Playground.


As side note, AFAICT, \u{feff} is the byte order mark for UTF-16, not UTF-8 which is the encoding Rust strings use. If your files are UTF-16 encoded and contain non-ASCII charakters, parsing your files as UTF-8 can fail in funny ways. I'm not too well read on unicode encodings, I just remember having to debug why my strings in Dart were looking funny when they came from my json API (Dart strings are UTF-16 and json is UTF-8 encoded).

1 Like

Work fine!. Jofas
a good practical example of the use WHILE LET.
Thanks a lot.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.