我目前正在开发一个简单的“用户输入”程序。用户可以输入一个数字,这是我使用std::io::stdin().read_line(&mut let_name_here).ok().expect("Error");得到的。在获得用户输入后,我希望将其打印到控制台进行审查。
我注意到println中有奇怪的行为!宏。下面的代码
println!("Your input: {}", let_name_here);
println!("Your input: {}", let_name_here);输出如下:
Your input: 15
Your input: 15为什么在println!中有一个额外的\n。根据我的编码经验,我会假设以下几点:
Your input: 15
Your input: 15但要实现此输出,我必须使用以下代码:
print!("Your input: {}", let_name_here);
print!("Your input: {}", let_name_here);我不明白为什么println! marco会输出两次\n。我应该怎么做,如果我想在第一行的末尾使用\n,这两个marcos是不可能的。我是不是错过了什么重要的东西?
发布于 2019-09-21 18:48:15
看起来在您从stdin读取的字符串的末尾有一个换行符。然后,println宏会在末尾添加一个换行符,从而产生两个换行符。
您应该去掉从stdin读取的文本末尾的换行符,或者如果您不想这样做,只需使用print。
https://stackoverflow.com/questions/58039413
复制相似问题