expression
Choose CPUs only within an allowed affinity mask
Linux CPU affinity constrains where a thread may run; it does not guarantee exclusive ownership or immediate execution. The effective CPU set can be restricted by available CPUs and cpusets. This model intersects a requested list with an illustrative allowed list, without querying or changing scheduling. Expected stdout (not executed): selected: [4, 6] Common mistake: Assuming CPU zero is available in every container, or equating affinity with core reservation. Run only in the isolated fixture runner, not a public code playground.
Formal Syntax & Structure
rust syntaxrust
sched_getaffinity / sched_setaffinity (Linux C APIs)Working Example
Example Snippetrust
fn main() { let allowed = [2, 4, 6]; let requested = [0, 4, 6]; let selected: Vec<_> = requested.into_iter().filter(|cpu| allowed.contains(cpu)).collect(); println!("selected: {selected:?}");}