mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-09-29 10:13:29 +00:00
feat: add async exercises
This commit is contained in:
+52
-12
@@ -1079,11 +1079,51 @@ original sending end.
|
||||
Related section in The Book:
|
||||
https://doc.rust-lang.org/book/ch16-02-message-passing.html"""
|
||||
|
||||
# ASYNC
|
||||
|
||||
[[exercises]]
|
||||
name = "async1"
|
||||
dir = "21_async"
|
||||
test = true
|
||||
hint = """
|
||||
Async functions are not the same as normal functions -- they have to be marked with a
|
||||
special bit of syntax, `async fn`. These functions don't immediately return or even
|
||||
execute, you have to encourage them to do so by calling another special bit of syntax
|
||||
on them.
|
||||
|
||||
Another thing - an async function can't be properly called in a normal function. Think of
|
||||
it as something contagious -- everything that it touches needs to be marked as such. Keeping
|
||||
that in mind, what adjustment do you need to make to the function signature?
|
||||
|
||||
Aside:
|
||||
`#[tokio::test]` (and `#[tokio::main]`) are "magic" attributes that automatically
|
||||
set up what we call an async runtime. The Rust compiler intentionally doesn't supply
|
||||
a default implementation of this runtime. Tokio is by far the most popular
|
||||
community-developed runtime, and this macro does a lot of the heavy lifting to let
|
||||
us use it.
|
||||
"""
|
||||
|
||||
[[exercises]]
|
||||
name = "async2"
|
||||
dir = "21_async"
|
||||
test = true
|
||||
hint = """
|
||||
Async functions can be used to run multiple things in parallel, or to more efficiently
|
||||
run things on multiple cores. Here, we use Tokio's tasks to schedule some work to run
|
||||
at the same time. We use a `JoinSet`, which is a list of tasks that lets us decide how
|
||||
to best execute them.
|
||||
|
||||
One of the ways to execute tasks is `JoinSet::join_all`, which even gives us a neat
|
||||
Vec that we can immediately return! You can also do this sequentially, with an iterator.
|
||||
See if you can also find a way to do it that doesn't use `JoinSet`! You have access to
|
||||
most of Tokio's task-based functionality here.
|
||||
"""
|
||||
|
||||
# MACROS
|
||||
|
||||
[[exercises]]
|
||||
name = "macros1"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
When you call a macro, you need to add something special compared to a regular
|
||||
@@ -1091,7 +1131,7 @@ function call."""
|
||||
|
||||
[[exercises]]
|
||||
name = "macros2"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
Macros don't quite play by the same rules as the rest of Rust, in terms of
|
||||
@@ -1102,7 +1142,7 @@ Unlike other things in Rust, the order of "where you define a macro" versus
|
||||
|
||||
[[exercises]]
|
||||
name = "macros3"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
In order to use a macro outside of its module, you need to do something
|
||||
@@ -1110,7 +1150,7 @@ special to the module to lift the macro out into its parent."""
|
||||
|
||||
[[exercises]]
|
||||
name = "macros4"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
You only need to add a single character to make this compile.
|
||||
@@ -1127,7 +1167,7 @@ https://veykril.github.io/tlborm/"""
|
||||
|
||||
[[exercises]]
|
||||
name = "clippy1"
|
||||
dir = "22_clippy"
|
||||
dir = "23_clippy"
|
||||
test = false
|
||||
strict_clippy = true
|
||||
hint = """
|
||||
@@ -1144,7 +1184,7 @@ appropriate replacement constant from `std::f32::consts`."""
|
||||
|
||||
[[exercises]]
|
||||
name = "clippy2"
|
||||
dir = "22_clippy"
|
||||
dir = "23_clippy"
|
||||
test = false
|
||||
strict_clippy = true
|
||||
hint = """
|
||||
@@ -1157,7 +1197,7 @@ https://doc.rust-lang.org/std/option/#iterating-over-option"""
|
||||
|
||||
[[exercises]]
|
||||
name = "clippy3"
|
||||
dir = "22_clippy"
|
||||
dir = "23_clippy"
|
||||
test = false
|
||||
strict_clippy = true
|
||||
hint = "No hints this time!"
|
||||
@@ -1166,20 +1206,20 @@ hint = "No hints this time!"
|
||||
|
||||
[[exercises]]
|
||||
name = "using_as"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Use the `as` operator to cast one of the operands in the last line of the
|
||||
`average` function into the expected return type."""
|
||||
|
||||
[[exercises]]
|
||||
name = "from_into"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Follow the steps provided right before the `From` implementation."""
|
||||
|
||||
[[exercises]]
|
||||
name = "from_str"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
The implementation of `FromStr` should return an `Ok` with a `Person` object,
|
||||
or an `Err` with an error if the string is not valid.
|
||||
@@ -1196,7 +1236,7 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
|
||||
|
||||
[[exercises]]
|
||||
name = "try_from_into"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
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?
|
||||
@@ -1206,6 +1246,6 @@ types?"""
|
||||
|
||||
[[exercises]]
|
||||
name = "as_ref_mut"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
|
||||
|
||||
Reference in New Issue
Block a user