fn read_cpu_times() -> Result<(u64, u64), Fail> {
    // open file
    let mut file = OpenOptions::new()
        .read(true)
        .open("/proc/stat")
        .or_else(Fail::from)?;

    // read file
    let mut buf = String::new();
    file.read_to_string(&mut buf).or_else(Fail::from)?;

    // only first line
    buf = {
        // split lines and get first
        let line = buf
            .split('\n')
            .next()
            .ok_or_else(|| Fail::new("broken /proc/stat"))?;

        // line should be at least 10 characters
        if line.len() < 10 {
            return Fail::from("broken /proc/stat");
        }

        // cut "cpu  "
        (&line[5..line.len()]).to_string()
    };

    // split by whitespace and get idle time
    let split: Vec<&str> = buf.split_ascii_whitespace().collect();
    let idle: u64 = split[3].parse().or_else(Fail::from)?;

    // calculate total from all times
    let mut total = 0u64;
    for s in split {
        total += s.parse::<u64>().or_else(Fail::from)?;
    }

    // return idle and total time
    Ok((idle, total))
}
/// Get CPU idle and total time from /proc/stat
Get CPU idle and total time from /proc/stat
Get CPU idle and total time from / proc / stat
fn read_cpu_times() -> Result<(u64, u64), Fail> {
    // open file
    let mut file = OpenOptions::new()
        .read(true)
        .open("/proc/stat")
        .or_else(Fail::from)?;
    // read file
    let mut buf = String::new();
    file.read_to_string(&mut buf).or_else(Fail::from)?;
    // only first line
    buf = {
        // split lines and get first
        let line = buf
            .split('\n')
            .next()
            .ok_or_else(|| Fail::new("broken /proc/stat"))?;
        // line should be at least 10 characters
        if line.len() < 10 {
            return Fail::from("broken /proc/stat");
        }
        // cut "cpu  "
        (&line[5..line.len()]).to_string()
    };
    // split by whitespace and get idle time
    let split: Vec<&str> = buf.split_ascii_whitespace().collect();
    let idle: u64 = split[3].parse().or_else(Fail::from)?;
    // calculate total from all times
    let mut total = 0u64;
    for s in split {
        total += s.parse::<u64>().or_else(Fail::from)?;
    }
    // return idle and total time
    Ok((idle, total))
}
fn read_cpu_times ( ) -> Result < ( u64 , u64 ) , Fail > { // open file let mut file = OpenOptions :: new ( ) . read ( true ) . open ( " " ) . or_else ( Fail :: from ) ? ; // read file let mut buf = String :: new ( ) ; file . read_to_string ( & mut buf ) . or_else ( Fail :: from ) ? ; // only first line buf = { // split lines and get first let line = buf . split ( '\n' ) . next ( ) . ok_or_else ( | | Fail :: new ( " " ) ) ? ; // line should be at least 10 characters if line . len ( ) < 10 { return Fail :: from ( " " ) ; } // cut "cpu  " ( & line [ 5 .. line . len ( ) ] ) . to_string ( ) } ; // split by whitespace and get idle time let split : Vec < & str > = buf . split_ascii_whitespace ( ) . collect ( ) ; let idle : u64 = split [ 3 ] . parse ( ) . or_else ( Fail :: from ) ? ; // calculate total from all times let mut total = 0u64 ; for s in split { total += s . parse :: < u64 > ( ) . or_else ( Fail :: from ) ? ; } // return idle and total time Ok ( ( idle , total ) ) }
Get CPU idle and total time from /proc/stat
Get CPU idle and total time from / proc / stat