get system cpu temperature in fahrenheit in rust

To get the system CPU temperature in Fahrenheit in Rust, we'll need to make a system call and parse the output. Here's an example code that works on Linux:

main.rs
use std::process::Command;

fn main() {
    let output = Command::new("sh")
        .arg("-c")
        .arg("sensors | grep 'CPU Temperature' | awk '{print $4}'")
        .output()
        .expect("failed to execute process");

    let output_str = String::from_utf8_lossy(&output.stdout);
    let celsius: f32 = output_str.trim().parse().unwrap();
    let fahrenheit = celsius * 1.8 + 32.0;

    println!("CPU temperature is {:.2}°F", fahrenheit);
}
451 chars
16 lines

This code runs the sensors command to get the system sensors, and then uses grep and awk to extract the CPU temperature in Celsius. It then converts this temperature to Fahrenheit and prints it to the console. Note that this code assumes that the CPU temperature is reported in Celsius and that the sensors command is available on the system.

gistlibby LogSnag