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.
Code
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
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.