expression

Preserve previous interrupt mask state

A critical section excludes specified competing execution contexts during a bounded operation. On a suitable single-core system an interrupt-masking implementation can protect against maskable interrupt handlers. Nesting must restore the prior state, not unconditionally enable interrupts. This boolean model is not a critical-section implementation and has no panic handling or hardware effects. Expected host stdout (not executed): inside outer: false after outer: true Common mistake: Unconditionally enabling interrupts on leaving an inner section, or treating the simulation as safe hardware code.

Formal Syntax & Structure

rust syntaxrust
save state → enter section → restore saved state

Working Example

Example Snippetrust
fn enter(enabled: &mut bool) -> bool { core::mem::replace(enabled, false) }fn restore(enabled: &mut bool, saved: bool) { *enabled = saved; }fn main() {    let mut enabled = true;    let outer = enter(&mut enabled);    let inner = enter(&mut enabled);    restore(&mut enabled, inner);    println!("inside outer: {enabled}");    restore(&mut enabled, outer);    println!("after outer: {enabled}");}
Rust Reference