Todo Unimplemented Unreachable Panic Stubs

todo, unimplemented, and unreachable panic with distinct messages; catch_unwind with a silenced hook shows each stub inside a dispatch table.

RustCli Tooling

Code

rustsnippet
28 lines
use std::panic::{catch_unwind, set_hook};

fn dispatch(command: &str) {
    match command {
        "greet" => println!("greet: hello"),
        "fetch" => todo!("fetch lands next sprint; stub keeps the match exhaustive"),
        "legacy" => unimplemented!("legacy import is planned but not started"),
        _ => unreachable!("arg parser whitelists commands before dispatch"),
    }
}

fn main() {
    // silence the default panic hook so payload printing stays ours
    set_hook(Box::new(|_| {}));

    for command in ["greet", "fetch", "legacy"] {
        match catch_unwind(|| dispatch(command)) {
            Ok(()) => println!("{command} -> ok"),
            Err(payload) => {
                let msg = payload
                    .downcast_ref::<String>()
                    .cloned()
                    .unwrap_or_default();
                println!("{command} -> panicked: {msg}");
            }
        }
    }
}

What the demo stubs

dispatch maps three commands to three panic macros: greet works, fetch panics via todo! with its sprint note, and legacy panics via unimplemented!. The match stays exhaustive because the stubs occupy arms that are not ready yet.

main silences the default panic hook so payload printing stays ours, then catch_unwind recovers each panic: the output prints greet -> ok, then the fetch and legacy messages carried in the String payload. The unreachable! arm documents the parser invariant without ever firing here.

Points to remember

  • todo! and unimplemented! differ only in intent wording.
  • Silencing the hook keeps the demo output clean, not the payload.

Interactive Tools for This Topic

FAQ

How does the demo read the panic payload?
catch_unwind returns Err(Box<dyn Any>); downcast_ref::<String>() extracts the macro message, which the printed panicked lines carry.