mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-06-30 08:18:45 +00:00
28 lines
769 B
Rust
28 lines
769 B
Rust
// This program fetches a greeting asynchronously. Async functions return a
|
|
// `Future` that must be `.await`ed to get the result. An async runtime like
|
|
// Tokio is needed to drive futures to completion.
|
|
use std::time::Duration;
|
|
|
|
async fn fetch_greeting() -> String {
|
|
trpl::sleep(Duration::from_millis(10)).await;
|
|
String::from("Hello, async world!")
|
|
}
|
|
|
|
fn main() {
|
|
trpl::block_on(async {
|
|
// TODO: We need to `await` the future returned by `fetch_greeting`.
|
|
let greeting = fetch_greeting();
|
|
println!("{greeting}");
|
|
});
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn fetch_greeting_returns_expected_message() {
|
|
trpl::block_on(async { assert_eq!(fetch_greeting().await, "Hello, async world!") });
|
|
}
|
|
}
|