get system cpu frequency in rust

Here's how to get the system CPU frequency in Rust using the cpuid crate:

main.rs
use cpuid::CpuId;

fn main() {
    let cpuid = CpuId::new();
    if let Some(frequency) = cpuid.get_processor_frequency_info() {
        println!("CPU Frequency: {} MHz", frequency.actual());
    } else {
        println!("CPU frequency information not available");
    }
}
274 chars
11 lines

Here, we're using the CpuId struct from the cpuid crate to get information about the CPU. The get_processor_frequency_info method returns an Option containing a cpuid::frequency::Info struct, which has the actual and maximum frequencies of the CPU. We're using the actual() method to get the actual frequency and printing it out.

related categories

gistlibby LogSnag