error[E0277]: `main` has invalid return type `impl std::future::Future`
--> src/main.rs:5:20
|
5 | async fn main() -> Result<(), ()>{
| ^^^^^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
|
= help: consider using `()`, or a `Result`
use tokio::net::TcpStream;
use tokio::prelude::*;
#[tokio::main]
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());
}
Thanks @steveklabnik / @naim / @jameseb7 / @asymmetrikon / @kornel
But there is something I did not understand, I thought async/await is part of the stable Rust now, so why I need to use external crate for it
What has been stabilized is the minimal feature set needed to enable the async await feature which requires rather extensive compiler support. Adding an executor to the standard library would mean that this specific choice of executor api is now locked down forever because of backwards compatibility. You may be interested in this thread.
Languages like JavaScript have a built-in event loop that is global, hardcoded and can't be customized, and JavaScript's Promise works only with that event loop.
Rust has separated interface of the Future (async/await) — an abstract concept of a function that doesn't run all at once, from the implementation of the event loop that runs these functions. You can choose how futures will be executed, and different implementations make different choices about performance, memory usage, etc.
So async/await as known from JavaScript and alike, isn't built into Rust. Only one half of it is.