Function num_cpus::get

source ·
pub fn get() -> usize
Expand description

Returns the number of available CPUs of the current system.

This function will get the number of logical cores. Sometimes this is different from the number of physical cores (See Simultaneous multithreading on Wikipedia).

This will always return at least 1.


let cpus = num_cpus::get();
if cpus > 1 {
    println!("We are on a multicore system with {} CPUs", cpus);
} else {
    println!("We are on a single core system");


This will check sched affinity on Linux, showing a lower number of CPUs if the current thread does not have access to all the computer’s CPUs.

This will also check cgroups, frequently used in containers to constrain CPU usage.

Examples found in repository?
examples/ (line 4)
fn main() {
    println!("Logical CPUs: {}", num_cpus::get());
    println!("Physical CPUs: {}", num_cpus::get_physical());