Subcommands map a command word to a specific action, with a fallback for unknown input.

Program

Play the program to choose a command and route it to an action string.

cli_subcommand_router.rs
fn main() {
    let command = ;
    let action = route(command);
    println!("{action}");
}

fn route(command: &str) -> &'static str {
    match command {
        "build" => "compile project",
        "test" => "run tests",
        "clean" => "remove artifacts",
        _ => "show help",
    }
}
fn main() {
    let command = ;
    let action = route(command);
    println!("{action}");
}

fn route(command: &str) -> &'static str {
    match command {
        "build" => "compile project",
        "test" => "run tests",
        "clean" => "remove artifacts",
        _ => "show help",
    }
}
fn main() {
    let command = ;
    let action = route(command);
    println!("{action}");
}

fn route(command: &str) -> &'static str {
    match command {
        "build" => "compile project",
        "test" => "run tests",
        "clean" => "remove artifacts",
        _ => "show help",
    }
}
subcommand The command word chooses the action branch.
router `route` centralizes the command-to-action mapping.
fallback A wildcard branch can handle unknown command words.