get system cpu temperature in rust

To get the system CPU temperature in Rust, we need to use a crate called sys-info. This crate provides functions to retrieve system information, including CPU temperature.

First, add sys-info to your Cargo.toml file:

[dependencies]
sys-info = "0.7.0"
34 chars
3 lines

Then, use the sys_info::loadavg() function to retrieve the load average information, which includes the CPU temperature:

main.rs
use sys_info::{loadavg, mem_info, system_info, ProcessInfo, SystemExt};

fn main() {
    let mut system = system_info().unwrap();
    let mem = mem_info().unwrap();
    let (one, five, fifteen) = loadavg().unwrap();

    // print CPU temperature
    println!("CPU temperature: {}°C", system.get_temperature());
}
313 chars
11 lines

Note that the get_temperature() function returns the temperature in Celsius.

gistlibby LogSnag