mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-01 08:19:18 +00:00
Merge ad19fae161af832ba8effb41254f464bb10d2b1c into 7c0d269279aaed64d6b8240ed5c1e9f6a981181e
This commit is contained in:
commit
1b35ab6b75
16
exercises/21_async/README.md
Normal file
16
exercises/21_async/README.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Async (Asynchronous Programming)
|
||||||
|
|
||||||
|
Asynchronous programming allows a program to perform tasks concurrently
|
||||||
|
without blocking the main execution thread. It is particularly useful
|
||||||
|
for I/O-bound operations, such as network requests or file reading,
|
||||||
|
where waiting for a response can be done in the background.
|
||||||
|
In Rust, asynchronous functions are defined using the async keyword
|
||||||
|
and are executed with the help of an asynchronous runtime like tokio.
|
||||||
|
This approach improves the efficiency and responsiveness of applications
|
||||||
|
by enabling them to handle multiple tasks simultaneously.
|
||||||
|
|
||||||
|
## Further information
|
||||||
|
|
||||||
|
- [Asynchronous Programming in Rust](https://doc.rust-lang.org/book/ch17-00-async-await.html)
|
||||||
|
- [Learn Tokio](https://tokio.rs/tokio/tutorial/)
|
||||||
|
- [Tokio Documentation](https://docs.rs/tokio/latest/tokio/)
|
||||||
34
exercises/21_async/async1.rs
Normal file
34
exercises/21_async/async1.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// 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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1079,11 +1079,27 @@ original sending end.
|
|||||||
Related section in The Book:
|
Related section in The Book:
|
||||||
https://doc.rust-lang.org/book/ch16-02-message-passing.html"""
|
https://doc.rust-lang.org/book/ch16-02-message-passing.html"""
|
||||||
|
|
||||||
|
# ASYNC
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "async1"
|
||||||
|
dir = "21_async"
|
||||||
|
hint = """
|
||||||
|
Remember what keyword is used to define an asynchronous function. The function
|
||||||
|
returns a `Future` that can be awaited.
|
||||||
|
|
||||||
|
A keyword is used to wait for the result of a `Future`. It can only
|
||||||
|
be used inside an asyncronous function.
|
||||||
|
|
||||||
|
For documentation on 'tokio::sleep', see:
|
||||||
|
https://docs.rs/tokio/latest/tokio/time/fn.sleep.html"""
|
||||||
|
|
||||||
|
|
||||||
# MACROS
|
# MACROS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "macros1"
|
name = "macros1"
|
||||||
dir = "21_macros"
|
dir = "22_macros"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
When you call a macro, you need to add something special compared to a regular
|
When you call a macro, you need to add something special compared to a regular
|
||||||
@ -1091,7 +1107,7 @@ function call."""
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "macros2"
|
name = "macros2"
|
||||||
dir = "21_macros"
|
dir = "22_macros"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
Macros don't quite play by the same rules as the rest of Rust, in terms of
|
Macros don't quite play by the same rules as the rest of Rust, in terms of
|
||||||
@ -1102,7 +1118,7 @@ Unlike other things in Rust, the order of "where you define a macro" versus
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "macros3"
|
name = "macros3"
|
||||||
dir = "21_macros"
|
dir = "22_macros"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
In order to use a macro outside of its module, you need to do something
|
In order to use a macro outside of its module, you need to do something
|
||||||
@ -1110,7 +1126,7 @@ special to the module to lift the macro out into its parent."""
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "macros4"
|
name = "macros4"
|
||||||
dir = "21_macros"
|
dir = "22_macros"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
You only need to add a single character to make this compile.
|
You only need to add a single character to make this compile.
|
||||||
@ -1127,7 +1143,7 @@ https://veykril.github.io/tlborm/"""
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "clippy1"
|
name = "clippy1"
|
||||||
dir = "22_clippy"
|
dir = "23_clippy"
|
||||||
test = false
|
test = false
|
||||||
strict_clippy = true
|
strict_clippy = true
|
||||||
hint = """
|
hint = """
|
||||||
@ -1144,7 +1160,7 @@ appropriate replacement constant from `std::f32::consts`."""
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "clippy2"
|
name = "clippy2"
|
||||||
dir = "22_clippy"
|
dir = "23_clippy"
|
||||||
test = false
|
test = false
|
||||||
strict_clippy = true
|
strict_clippy = true
|
||||||
hint = """
|
hint = """
|
||||||
@ -1157,7 +1173,7 @@ https://doc.rust-lang.org/std/option/#iterating-over-option"""
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "clippy3"
|
name = "clippy3"
|
||||||
dir = "22_clippy"
|
dir = "23_clippy"
|
||||||
test = false
|
test = false
|
||||||
strict_clippy = true
|
strict_clippy = true
|
||||||
hint = "No hints this time!"
|
hint = "No hints this time!"
|
||||||
@ -1166,20 +1182,20 @@ hint = "No hints this time!"
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "using_as"
|
name = "using_as"
|
||||||
dir = "23_conversions"
|
dir = "24_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
Use the `as` operator to cast one of the operands in the last line of the
|
Use the `as` operator to cast one of the operands in the last line of the
|
||||||
`average` function into the expected return type."""
|
`average` function into the expected return type."""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "from_into"
|
name = "from_into"
|
||||||
dir = "23_conversions"
|
dir = "24_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
Follow the steps provided right before the `From` implementation."""
|
Follow the steps provided right before the `From` implementation."""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "from_str"
|
name = "from_str"
|
||||||
dir = "23_conversions"
|
dir = "24_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
The implementation of `FromStr` should return an `Ok` with a `Person` object,
|
The implementation of `FromStr` should return an `Ok` with a `Person` object,
|
||||||
or an `Err` with an error if the string is not valid.
|
or an `Err` with an error if the string is not valid.
|
||||||
@ -1196,7 +1212,7 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "try_from_into"
|
name = "try_from_into"
|
||||||
dir = "23_conversions"
|
dir = "24_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
Is there an implementation of `TryFrom` in the standard library that can both do
|
Is there an implementation of `TryFrom` in the standard library that can both do
|
||||||
the required integer conversion and check the range of the input?
|
the required integer conversion and check the range of the input?
|
||||||
@ -1206,6 +1222,6 @@ types?"""
|
|||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "as_ref_mut"
|
name = "as_ref_mut"
|
||||||
dir = "23_conversions"
|
dir = "24_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
|
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
|
||||||
|
|||||||
30
solutions/21_async/async1.rs
Normal file
30
solutions/21_async/async1.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use tokio::time::{sleep, Duration};
|
||||||
|
|
||||||
|
// Change the function signature to be async and return a Future
|
||||||
|
async fn delayed_hello() -> String {
|
||||||
|
// Wait for 1 second
|
||||||
|
sleep(Duration::from_secs(1)).await;
|
||||||
|
|
||||||
|
"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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user