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);
}
}
- Create a new binary project with
cargo new my_repl. - Replace the contents of
src/main.rswith the code above. - Run the program with
cargo runto start the interactive loop.