mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-11-29 22:47:43 +02:00
feat: add async exercises
This commit is contained in:
44
exercises/21_async/async1.rs
Normal file
44
exercises/21_async/async1.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
// Our loyal worker works hard to create a new number.
|
||||
#[derive(Default)]
|
||||
struct Worker;
|
||||
|
||||
struct NumberContainer {
|
||||
number: i32,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
async fn work(&self) -> NumberContainer {
|
||||
// Pretend this takes a while...
|
||||
let new_number = 32;
|
||||
NumberContainer { number: new_number }
|
||||
}
|
||||
}
|
||||
|
||||
impl NumberContainer {
|
||||
async fn extract_number(&self) -> i32 {
|
||||
// And this too...
|
||||
self.number
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Fix the function signature!
|
||||
fn run_worker() -> i32 {
|
||||
// TODO: Make our worker create a new number and return it.
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Feel free to experiment here. You may need to make some adjustments
|
||||
// to this function, though.
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Don't worry about this attribute for now.
|
||||
// If you want to know what this does, read the hint!
|
||||
#[tokio::test]
|
||||
async fn test_if_it_works() {
|
||||
let number = run_worker().await;
|
||||
assert_eq!(number, 32);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user