How can I get the temperature of a PC’s CPU in Rust?
I ultimately want to get the temperature (in celsius) every five minutes and log it to the console.
I am targeting Windows 10 20H2 and have Rust version 1.75.0
.
Try something like this:
use std::{thread, time::Duration};
use sysinfo::{System, SystemExt, ComponentExt};
fn main() {
let mut system= System::new_all();
loop {
system.refresh_components();
let cpu_temp = system.get_components()
.iter()
.filter(|component| component.get_label().contains("CPU"))
.map(|component| component.get_temperature())
.next();
match cpu_temp {
Some(temp) => println!("Temp. of CPU: {:.2} °C", temp),
None => println!("temperature not available."),
}
thread::sleep(Duration::from_secs(300));
}
}