1

I'm trying to make a program that reads any word on the command line and prints it on a new line that's all, what I'm trying to do is something like this:

Some text: hello

But instead I get something like this:

Some text:
Hello

Since using print! instead of println! the phrase "some text" is not printed.

What's going on?

fn main() {
    loop {
        let mut linea = String::new();
        print!("something:\t");
        let ingreso = std::io::stdin().read_line(&mut linea).unwrap();
        println!("{:?}",ingreso);
        if linea == "exit".to_string(){
            break;
        }
    }
}
E_net4
  • 27,810
  • 13
  • 101
  • 139

1 Answers1

3

print! does not flush stdout. You need to import the std::io::Write trait and call std::io::stdout().flush().

Also, the return value from read_line returns how many bytes where read. If you want to print the text that was entered, print the linea variable.

pigeonhands
  • 3,066
  • 15
  • 26