LoginSignup
1
0

色々な言語で簡単なREPLを実装する #rust

Last updated at Posted at 2023-12-05

続きです。

REPLとは

こちらの記事を参照してください。

Rustで実装する

$ cargo new rs-repl

src/main.rs

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

fn main() {
    let mut input = String::new();

    loop {
        print!("rust> ");
        io::stdout().flush().unwrap();
        input.clear();
        io::stdin().read_line(&mut input).unwrap();

        let prompt = input.trim();
        match prompt {
            "exit" => {
                println!("Bye!");
                break;
            },
            _ => println!("{}", prompt),
        }
    }
}

実行する

$ cargo build
$ cargo run
rust> 1 + 1
1 + 1
rust> foobar
foobar
rust> baz
baz
rust> exit
Bye!

こちらもctrl-cとctrl-dの処理を考えておきたいと思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0