Does the Rust language support non-blocking IO in its standard? I searched the internet, but couldn't find anything related to its standard.
Asked
Active
Viewed 4,417 times
10
-
4*"I searched the internet, but couldn't find anything."* - I have no idea what kind of search engine you are using or if you are behind some firewall which specifically filters out searching for this topic - but using Google with [rust non blocking io](https://www.google.com/search?q=rust+non+blocking+io) returns lots of useful results. Hint: it is well supported but it is not called nio. It isn't called nio in almost all languages. – Steffen Ullrich Jan 26 '20 at 12:46
-
1@SteffenUllrich As I mentioned in the question, I need NIO provided by Rust's standard, Not a third-party implemented project. If it's called something different in Rust. So what is the keyword for it? – Amir Fo Jan 26 '20 at 12:58
-
3[tokio.rs](https://tokio.rs) might be what you are looking for. Note that the standard library is intended to be [_"a set of minimal, battle-tested shared abstractions for the broader Rust ecosystem"_](https://doc.rust-lang.org/std/). Relying on additional crates for key functionalities (e.g. regex, RNGs, ...) is much more common than in Java. – E_net4 Jan 27 '20 at 12:36
3 Answers
15
Does the Rust language support non-blocking IO (NIO) in its standard?
No.
Rust developers have a different philosophy than Java developers, and prefer to keep a rather minimalist standard library. Especially when there is no obvious "best" trade-off.
At the moment, non-blocking I/O requires the use of 3rd-party crates such as Tokio or async-std. While more async
functionality may be integrated into the standard library in the future, no plan has been made to integrate anything beyond vocabulary traits.

Matthieu M.
- 287,565
- 48
- 449
- 722
-
I find [smol](https://github.com/smol-rs/smol) lib quite good too. – SergeiMinaev Sep 29 '21 at 13:58
4
But Yes! It's called tokio
using async
in Rust.
use tokio::net::TcpStream;
use tokio::prelude::*;
async fn main() {
let mut stream = TcpStream::connect("127.0.0.1:6142").await.unwrap();
println!("created stream");
let result = stream.write(b"hello world\n").await;
println!("wrote to stream; success={:?}", result.is_ok());
}
Await called in this example writes the message asynchronously. [1]: https://tokio.rs
-
@Shepmaster I got it from tokio's doc [here](https://tokio.rs/docs/getting-started/hello-world/) – Jan 29 '20 at 15:40
-
2The question specifically asks if **the Rust language** supports **non-blocking IO**. The answer to that is no, as stated in the [upvoted answer](https://stackoverflow.com/a/59918662/155423). `async` does not provide IO. This code shows **third-party** crates that use `async` to provide non-blocking IO. See [What is the purpose of async/await in Rust?](https://stackoverflow.com/q/52835725/155423) for further details. – Shepmaster Jan 29 '20 at 15:41
-
1You may have started from Tokio's doc, but you seemingly decided to change it instead of copying it directly. If you copied something from another site, you should attribute it by linking to the source and using the blockquote syntax. – Shepmaster Jan 29 '20 at 15:43