get system cpu count in rust

To get the number of CPU cores in the current system using Rust, you can use the sysinfo crate.

First, you need to add the sysinfo crate to your Cargo.toml file:

[dependencies]
sysinfo = "0.13.5"
34 chars
3 lines

Then, in your Rust code, you can use the following code snippet to get the number of CPU cores:

main.rs
use sysinfo::{System, SystemExt};

fn main() {
    let system = System::new_all();
    let cpu_count = system.get_processors().len();
    println!("Number of CPU cores: {}", cpu_count);
}
188 chars
8 lines

The System struct provided by the sysinfo crate represents the current system and provides information such as CPU count, memory usage, etc. The get_processors() method returns a vector of Processor structs, and the len() method returns the number of elements in the vector, which corresponds to the number of CPU cores.

related categories

gistlibby LogSnag