如何在 Rust 中没有尾随换行符的情况下打印输出? [英] How do I print output without a trailing newline in Rust?

查看:19
本文介绍了如何在 Rust 中没有尾随换行符的情况下打印输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust 中的宏 println! 总是在每个输出的末尾留下一个换行符.例如

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

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

给出输出

Enter the number : 
56

我不希望用户的输入 56 位于新行上.我该怎么做?

I don't want the user's input 56 to be on a new line. How do I do this?

推荐答案

您可以使用 print! 代替.

You can use the print! macro instead.

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

当心:

请注意,默认情况下标准输出经常是行缓冲的,因此可能需要使用 io::stdout().flush() 以确保立即发出输出.

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.

这篇关于如何在 Rust 中没有尾随换行符的情况下打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆