As I wasn't able to find a minimal, working code sample demonstrating the use of the brand-new async/await feature in Rust, I've written one myself. Here it is.
cargo.toml
:
[package] name = "asynchello" version = "0.1.0" authors = [ "code@mulk.eu" ] [dependencies.futures-core] git = "https://github.com/rust-lang-nursery/futures-rs" branch = "0.3" [dependencies.futures-executor] git = "https://github.com/rust-lang-nursery/futures-rs" branch = "0.3"
asynchello.rs
:
#![feature(await_macro, async_await, futures_api, pin)] extern crate futures_core; extern crate futures_executor; use std::boxed::PinBox; use std::task::Executor; use std::future::Future; use std::future::FutureObj; async fn compute_name() -> String { "world".into() } async fn say_hello() -> () { let name = await!(compute_name()); println!("Hello {}.", name); } fn make_task<F>(f: F) -> FutureObj<'static, ()> where F: 'static + Future<Output = ()> + Send { let future = PinBox::new(f); FutureObj::new(future) } fn main() { let task = make_task(say_hello()); println!("Running."); let mut pool = futures_executor::LocalPool::new(); let mut executor = pool.executor(); executor.spawn_obj(task).expect("oops, failed the task"); pool.run(&mut executor); }
Update 2018-07-16: Updated for latest nightly.
Comments
Submit a comment
Note: This website uses a JavaScript-based spam prevention system. Please enable JavaScript in your browser to post comments. Comment format is plain text. Use blank lines to separate paragraphs.