Home > other >  Rust: try to read file content else string
Rust: try to read file content else string

Time:09-24

I would like to read a specific line of a file, if the:

  • file does not exist
  • file cannot be open
  • the number of lines is less that the one that I want

just return an empty string.

Consider the following examples:

let wifi = "".to_string();
if let Ok(wifi_file) = fs::read_to_string("/proc/net/wireless") {
    if let Some(wifi_level) = wifi_file.lines().nth(2) {
        let wifi = format!("{:.0} dBa ", wifi_level
            .split_whitespace()
            .collect::<Vec<&str>>()[3]
            .parse::<f32>()
            .unwrap()
        );
    }
}
// wifi is out of scope
let wifi = match fs::read_to_string("/proc/net/wireless") {
    Ok(s) => format!("{:.0} dBA", s.lines()
        .nth(2)
        .expect("Unable to open")
        .split_whitespace()
        .collect::<Vec<&str>>()[3]
        .parse::<f32>()
        .unwrap()),
    Err(_) => "".to_string(),
};
// thread 'main' panicked at 'Unable to open', ...

Am I missing something obious about match?

Thanks

CodePudding user response:

In your first code, you are declaring wifi variable again instead of using the already declared variable.

Working code:

let mut wifi = "".to_string();
    if let Ok(wifi_file) = fs::read_to_string("/proc/net/wireless") {
        if let Some(wifi_level) = wifi_file.lines().nth(2) {
            wifi = format!("{:.0} dBa ", wifi_level
                .split_whitespace()
                .collect::<Vec<&str>>()[3]
                .parse::<f32>()
                .unwrap()
            );
        }
    }

Second code is working without any changes.

  • Related