因此,下面的代码应首先打印Enter a url:
,然后从用户那里获取输入,并在获取该输入之后,应将URL打印回去。
use std::io;
fn main() {
print!("Enter a url: ");
let mut url = String::new();
io::stdin().read_line(&mut url).expect("Failed to read line");
println!("The url was: {}", url)
}
但是输出就像:
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello`
google.com
Enter a url: The url was: google.com
print!
是异步的,而read_line
是同步的,这就是为什么它先阻塞然后打印的原因?