How do I print output without a trailing newline in Rust?

前端 未结 2 1094
孤城傲影
孤城傲影 2020-12-09 14:52

The macro println! in Rust always leaves a newline character at the end of each output. For example

println!(\"Enter the number : \");
io::stdin         


        
2条回答
  •  执笔经年
    2020-12-09 15:11

    You can use the print! macro instead.

    print!("Enter the number : ");
    io::stdin().read_line(&mut num);
    

    Beware:

    Note that stdout is frequently line-buffered by default so it may be necessary to use io::stdout().flush() to ensure the output is emitted immediately.

提交回复
热议问题