0

I have this function in the main.rs file in my code.

fn repl(vm: &VM) {
    loop {
        print!("> ");
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();

        vm::interpret(vm, &input);
    }
}

When I run the program, the print!("> ") line does not execute. However, when I take out the subsequent code it does. I've tried similar code from a different implementation I found online (see below), but even that does not work. The user input is working, however. Is there a reason why this happens? I'm new to Rust, so I don't have any ideas of what may be causing this.

Alternate Code Found:

fn repl(machine: &mut vm::VM) -> () {
   loop {
        print!("> ");
        std::io::stdout().flush().unwrap();

        let mut input = String::new();
        let expression = std::io::stdin().read_line(&mut input);
        match expression {
            Ok(_) => (),
            Err(_) => {
                println!("Error!");
               break;
            }
        }
        vm::interpret(vm, &input);
    }
}
  • 1
    Does the other example work? And does your code work if you add `std::io::stdout().flush().unwrap()` after you print "> ", like the other example does? – piojo Dec 04 '21 at 05:02
  • @piojo I'm getting this error with that line: no method named `flush` found for struct `std::io::Stdout` in the current scope method not found in `std::io::Stdout` Edit: I found the answer here https://stackoverflow.com/questions/40392906/no-method-named-flush-found-for-type-stdiostdout-in-the-current-scope . –  Dec 04 '21 at 05:19
  • 1
    `flush()` is available as part of the `std::io::Write` trait, so you just need to import it like so: [playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=461a0ed8bd8b7a0067503f90dc129f8e) – kmdreko Dec 04 '21 at 05:24

0 Answers0