How do I print STDOUT and get STDIN on the same line in Rust?
宏
可以安全地假设,如果连续使用
这是我要完成的示例:
1 2 3 4 5 6 7 | use std::io; fn main() { let mut input = String::new(); print!("Enter a string >>"); io::stdin().read_line(&mut input).expect("Error reading from STDIN"); } |
所需的输出将是(
1 | Enter a string >> STDIN |
实际输出为:
1 2 | STDIN Enter a string >> |
另一方面,
1 2 | Enter a string >> STDIN |
在Python(3.x)中,这可以用一行完成,因为
在Rust文档中找不到能够允许类似于Python示例的解决方案之后,我将任务分为了
例如
1 2 3 4 5 6 7 8 | use std::io::{self, Write}; fn main() { let mut input = String::new(); print!("Enter a string >>"); let _ = io::stdout().flush(); io::stdin().read_line(&mut input).expect("Error reading from STDIN"); } |
you should be able to write output and get input on the same line.
In Python (3.x), this can be accomplished with a single line, because the input function allows for a string argument that precedes the STDIN prompt:
variable = input("Output string")
好吧,这里您去:
1 2 3 4 | use dialoguer::Input; let name = Input::new().with_prompt("Your name").interact()?; println!("Name: {}", name); |
- https://docs.rs/dialoguer/0.3.0/dialoguer/struct.Input.html#example-usage