3 ms·
let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); And I just looked it up and it's right there in the docs, too: https://doc.ru
by ComputerGuru 2y ago
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
And I just looked it up and it's right there in the docs, too: https://doc.rust-lang.org/std/io/struct.Stdin.html https://doc.rust-lang.org/std/io/struct.Stdin.html
You basically shouldn't use `tokio::io::stdin()` for what you want, as is documented at https://docs.rs/tokio/latest/tokio/io/fn.stdin.html https://docs.rs/tokio/latest/tokio/io/fn.stdin.html
> For interactive uses, it is recommended to spawn a thread dedicated to user input and use blocking IO directly in that thread.
So the code under tokio would be
let line = rt.spawn_blocking(|| {
use std::io::stdin;
let mut buffer = String::new();
stdin().read_line(&mut buffer).unwrap()
}).await.unwrap();
- apitman 2y agoYeah std::io is what I ended up using, but it felt like I might be doing something wrong. Wasn't sure if using blocking IO would mess with tokio. Your answer seems to indicate that intuition was correct, and I should be wrapping it in spawn_blocking. That all makes sense, it would just be nice if your comment was at the top of the google search results.