Hi everyone, I'm new to Rust
and I'm porting my high-performance network service into Rust
as a POC to have a try. As I need performance, then I'm very interested on the rust async
.
After I went through the some videos in Rust conf
and read the doc in async-std
, I still quite got some questions which I hope that you guys can help me to solve or give me some idear:)
-
I choose
async-std
because it wrap bascially all thestd
lib inasync/await
support. But why I still seefutures
crate needed inhttps://book.async.rs/
. As I thoughtasync-std
should be a single lib I should use and I got everything. It's quite confuse me, as sometime I useasync-std
, and sometime I have to usefuture
crate (like thejoin!
andselect!
macro)..... Any deep explanation for this plz? -
All the examples told me that I can use
async block
to create aFuture
instance which I can.await
on it. Could some one to explain to me what big different with the followed code, as they all get the same result:
All done, future 1 result, future 2 result
Some team member ask me, honestly, I really don't know how to answer that question, I'm confusing When to use which way
, plz help
type FutureResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
///
/// `#[async_std::main]` attribute is enabled in `Cargo.toml` (features = ["attributes"] )
/// After that, we can mark the `main` function as `async`, then we can call `.await` inside the
/// function body :)
///
// Version 1
#[async_std::main]
async fn main() -> FutureResult<()> {
let fut_1 = async {
task::sleep(std::time::Duration::from_secs(1));
"future 1 result".to_string()
};
let fut_2 = async {
task::sleep(std::time::Duration::from_secs(1));
"future 2 result".to_string()
};
task::block_on(async {
println!("All done, {}, {}", fut_1.await, fut_2.await);
});
Ok(())
}
// Version 2
#[async_std::main]
async fn main() -> FutureResult<()> {
let fut_1 = task::spawn(async {
task::sleep(std::time::Duration::from_secs(1));
"future 1 result".to_string()
});
let fut_2 = task::spawn(async {
task::sleep(std::time::Duration::from_secs(1));
"future 2 result".to_string()
});
task::block_on(async {
println!("All done, {}, {}", fut_1.await, fut_2.await);
});
Ok(())
}
// Version 3
#[async_std::main]
async fn main() -> FutureResult<()> {
async fn fut_1() -> String {
task::sleep(std::time::Duration::from_secs(1));
"future 1 result".to_string()
};
async fn fut_2() -> String {
task::sleep(std::time::Duration::from_secs(1));
"future 2 result".to_string()
};
task::block_on(async {
println!("All done, {}, {}", fut_1().await, fut_2().await);
});
Ok(())
}
// Version 4
#[async_std::main]
async fn main() -> FutureResult<()> {
async fn fut_1() -> String {
task::sleep(std::time::Duration::from_secs(1));
"future 1 result".to_string()
};
async fn fut_2() -> String {
task::sleep(std::time::Duration::from_secs(1));
"future 2 result".to_string()
};
let fut_1_task = task::spawn(fut_1());
let fut_2_task = task::spawn(fut_2());
task::block_on(async {
println!("All done, {}, {}", fut_1_task.await, fut_2_task.await);
});
Ok(())
}