Code Example Template
match event {
    KeyEvent::Down(code) if active_app == "Photoshop" => trigger_ps_macro(code),
    KeyEvent::Down(code) => trigger_global_action(code),
}

Deep-Dive System Documentation

Context Routing with Match Guards

A Match Guard is an extra if condition attached to a match arm. The pattern matching must succeed and the guard condition must evaluate to true for the arm to trigger.

Why Match Guards?

Pattern matching only checks structures and variants known at compile time. It cannot validate dynamic values or ambient operating system states. Match guards close this gap cleanly.

// Advanced system key event router using dynamic app context
let active_app = get_active_window_title();

match event {
    KeyEvent::Down(code) if active_app == "Photoshop" => {
        execute_photoshop_macro(code);
    }
    KeyEvent::Down(code) if is_admin_process() => {
        execute_privileged_macro(code);
    }
    KeyEvent::Down(code) => {
        execute_default_shortcut(code);
    }
}

Key Details

  • Shared Borrows: The match guard can reference variables bound in the pattern without moving ownership.
  • Exhaustiveness: The compiler does not verify if guard conditions are mutually exclusive, so you must always supply a final default arm.

Quick Reference Guide

match num {
    x if x > 0 => "positive",
    _ => "other",
}