How to Write a REPL in Rust

Cli
Build a Rust REPL by creating a loop that reads stdin, processes input, and prints output until the user types quit.

You build a REPL in Rust by creating a loop that reads user input, processes it, and prints a response until the user exits.

use std::io::{self, Write};

fn main() {
    let mut input = String::new();
    loop {
        print!("> ");
        io::stdout().flush().unwrap();
        input.clear();
        io::stdin().read_line(&mut input).unwrap();
        let input = input.trim();
        if input == "quit" { break; }
        println!("You said: {}", input);
    }
}
  1. Create a new binary project with cargo new my_repl.
  2. Replace the contents of src/main.rs with the code above.
  3. Run the program with cargo run to start the interactive loop.