← All Workshops

MudEngine Part 2: Single-Player REPL

Step 8 / 10

The REPL loop

The REPL (Read-Eval-Print Loop) is the heart of our single-player MUD. It:

  1. Prints a prompt (>)
  2. Reads a line from stdin
  3. Trims and lowercases the input
  4. Splits into command and optional argument
  5. Dispatches to the appropriate handler
  6. Prints the result — then repeats
mud-engine-repl/src/main.rs
fn main() {
    let mut world = World::new();
    println!("=== MudEngine REPL ===\n");
    println!("{}\n", world.look());

    loop {
        print!("> ");
        io::stdout().flush().unwrap();

        let mut input = String::new();
        if io::stdin().read_line(&mut input).is_err() {
            break;
        }

        let input = input.trim().to_lowercase();
        if input.is_empty() {
            continue;
        }

        let parts: Vec<&str> = input.splitn(2, ' ').collect();
        match parts[0] {
            "quit" | "exit" => {
                println!("Farewell, adventurer!");
                break;
            }
            "look" | "l" => println!("{}", world.look()),
            "north" | "n" | "south" | "s" | "east" | "e" | "west" | "w" => {
                let dir = match parts[0] {
                    "n" => "north",
                    "s" => "south",
                    "e" => "east",
                    "w" => "west",
                    d => d,
                };
                println!("{}", world.go(dir));
            }
            "help" | "?" => {
                println!("Commands:");
                println!("  look / l            — describe the current room");
                println!("  north/n  south/s");
                println!("  east/e   west/w     — move in a direction");
                println!("  help / ?            — show this help");
                println!("  quit / exit         — leave the game");
            }
            _ => println!("Unknown command. Type 'help' or '?' for a list."),
        }
    }
}
💡 Short aliases

Real MUDs let players type l instead of look and n instead of north. We support the same shortcuts — a tiny quality-of-life touch that makes the REPL feel like a real MUD. The match arms handle both the full word and the shorthand, normalizing to the full word before passing to world.go().

Step 8 / 10