mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-29 07:19:17 +00:00
- Renamed/ordered the directories for macros, clippy, and conversions, shifting them all one up in numerical order to fit async as number 21 - Added the 21_async directories in exercises and solutions - Added async1.rs in the 21_async directory in exercises and solutions - Added async to the info.toml in rustlings-macros and reordered the rest of the exercises in there as well - Created an exercise for async1 and its solution
34 lines
872 B
Rust
34 lines
872 B
Rust
// Modify delayed_hello to return the string "Hello, world!"
|
|
// after waiting for 1 second to pass the test and fix the
|
|
// compiler error.
|
|
|
|
use tokio::time::{sleep, Duration};
|
|
|
|
// TODO: Change the function signature to fix the compiler error
|
|
fn delayed_hello() -> String {
|
|
// TODO: Return the string "Hello, world!" after waiting for 1 second
|
|
// ...
|
|
|
|
"Hello, world!".to_string()
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// You can experiment optionally here
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use tokio::time::Duration;
|
|
|
|
#[tokio::test]
|
|
async fn test_delayed_hello() {
|
|
let start = std::time::Instant::now();
|
|
let result = delayed_hello().await;
|
|
let duration = start.elapsed();
|
|
assert_eq!(result, "Hello, world!");
|
|
assert!(duration >= Duration::from_secs(1));
|
|
println!("Test passed!");
|
|
}
|
|
} |