Skip to content

Commit 73832d3

Browse files
committed
Add a basic example for a closure expression
1 parent 1106780 commit 73832d3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/expressions/closure-expr.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ Closures marked with the `async` keyword indicate that they are asynchronous in
4242

4343
Calling the async closure does not perform any work, but instead evaluates to a value that implements [`Future`] that corresponds to the computation of the body of the closure.
4444

45+
```rust
46+
async fn takes_async_callback(f: impl AsyncFn(u64)) {
47+
f(0).await;
48+
f(1).await;
49+
}
50+
51+
async fn example() {
52+
takes_async_callback(async |i| {
53+
core::future::ready(i).await;
54+
println!("done with {i}.");
55+
}).await;
56+
}
57+
```
58+
4559
> **Edition differences**: Async closures are only available beginning with Rust 2018.
4660
4761
## Example

0 commit comments

Comments
 (0)