From dac4d5c5826b2a17783c3f8bc0ce26c69638ad7c Mon Sep 17 00:00:00 2001 From: Luc Street Date: Wed, 30 Sep 2020 14:12:49 -0700 Subject: [PATCH] Add sheet for async programming in Rust --- sheets/_rust/async | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sheets/_rust/async diff --git a/sheets/_rust/async b/sheets/_rust/async new file mode 100644 index 0000000..b4a88a0 --- /dev/null +++ b/sheets/_rust/async @@ -0,0 +1,37 @@ +// The core of async is the `Future` trait, which defines some asynchronous computation +// that may eventually yield a value. +// +// For more info, see https://doc.rust-lang.org/stable/std/future/trait.Future.html + +// Functions can be marked `async` to indicate that they return a type implementing `Future`. +// The function signatures below are analogous. +async fn do_work() -> String { ... } +fn do_work() -> impl Future { ... } + +// To run a future, we can add a package that provides an async runtime, since Rust does +// not come with one in the standard library. An asynchronous runtime schedules futures +// and continuously polls them to completion. Here, we'll use tokio, a mature and +// battle-tested library. + +// in Cargo.toml +tokio = { version = "0.2.22", features = ["full"] } + +// Now we can spawn futures on a runtime. +fn main() { + let mut runtime = tokio::runtime::Runtime::new().unwrap(); + // `block_on` will block the current thread until the given task completes + runtime.block_on(async { + // Now we're in a worker thread + // Futures can be executed using `.await` + let my_string = do_work().await; + }); +} + +// We can also use a macro to make this much more compact. +#[tokio::main] +async fn main() { + let my_string = do_work().await; +} + +// See https://docs.rs/tokio for more information on tokio. +// See https://docs.rs/futures for more tools on working with futures.