|
| 1 | +- # Rust async |
| 2 | + - Here's a clean, Rust-native explanation of the selected line and the constructs involved. |
| 3 | + - ## 🔍 The selected line |
| 4 | + - ~~~rust |
| 5 | + let _cleanup = runtime.block_on(async { client.delete_project(project_id).await }); |
| 6 | + ~~~ |
| 7 | + - This combines **four** concepts: |
| 8 | + - **`async { ... }` block** |
| 9 | + - **`.await` operator** |
| 10 | + - **A future value** |
| 11 | + - **A synchronous executor call (`block_on`)** |
| 12 | + - Let's break those down. |
| 13 | + - ## 1. `async { ... }` — an async block expression |
| 14 | + - `async { ... }` is an **expression** that returns a *future* — specifically an *anonymous type that implements `Future<Output = T>`*. |
| 15 | + - Inside an async block, you can use `await`, but the block itself **does not run** until something *polls* the future (usually an executor). |
| 16 | + - It is similar to: |
| 17 | + - ~~~rust |
| 18 | + async fn foo() -> T { ... } |
| 19 | + ~~~ |
| 20 | + - but inline. |
| 21 | + - Other Rust constructs that work the same way (i.e., block forms that produce values) include: |
| 22 | + - `unsafe { ... }` |
| 23 | + - `const { ... }` |
| 24 | + - `try { ... }` (unstable; returns `Result`) |
| 25 | + - `loop { ... }`, `if { ... }`, etc., because *blocks are expressions* |
| 26 | + - Rust's rule: **Blocks are expressions that evaluate to the last expression inside.** |
| 27 | + - So the async block: |
| 28 | + - ~~~rust |
| 29 | + async { |
| 30 | + client.delete_project(project_id).await |
| 31 | + } |
| 32 | + ~~~ |
| 33 | + - produces a future whose output type is whatever `.await` returns — here likely `Result<(), SomeError>` or whatever the API defines. |
| 34 | + - ## 2. What is `.await`? |
| 35 | + - `.await` is **neither a method nor a property** — it is a **postfix operator built into the language parser**, like `?`. |
| 36 | + - It transforms a `Future<Output = T>` into a `T`, *but only inside an async context*. |
| 37 | + - If you want an analogy: |
| 38 | + - It is like Python `await f()`, **but syntactically resembles a field access** (`obj.field`). |
| 39 | + - Under the hood, it compiles down to state-machine code that polls the future and suspends. |
| 40 | + - Its "type"? |
| 41 | + - The expression: |
| 42 | + - ~~~rust |
| 43 | + some_future.await |
| 44 | + ~~~ |
| 45 | + - has the type **`T`**, where the original future was `Future<Output = T>`. |
| 46 | + - ## 3. The sequence of evaluation |
| 47 | + - Your full line: |
| 48 | + - ~~~rust |
| 49 | + let _cleanup = runtime.block_on(async { client.delete_project(project_id).await }); |
| 50 | + ~~~ |
| 51 | + - ⚙️ *Steps:* |
| 52 | + - `async { ... }` creates a future of type `impl Future<Output = DeleteResult>`. |
| 53 | + - `runtime.block_on(...)` takes that future and **runs it to completion**, turning async → sync. |
| 54 | + - The result of the `.await` inside is returned out of the async block. |
| 55 | + - That value becomes `_cleanup`. |
| 56 | + - ## 4. Are there other parts of Rust with similar syntax? |
| 57 | + - Yes — Rust has several "block operators" and "postfix operators": |
| 58 | + - ### **Block expressions:** |
| 59 | + - `async { … }` — future-producing |
| 60 | + - `unsafe { … }` — unsafe block |
| 61 | + - `try { … }` — result-producing block (unstable) |
| 62 | + - `const { … }` — const-evaluated block (stable) |
| 63 | + - `{ … }` plain blocks |
| 64 | + - ### **Postfix operators (built-in, not methods):** |
| 65 | + - `.await` |
| 66 | + - `?` |
| 67 | + - Array indexing (`arr[idx]`) |
| 68 | + - Deref and reference are prefix/suffix but part of expression syntax, not methods. |
| 69 | + - These are all *language-level constructs*, unlike Python where most constructs desugar into method calls. |
| 70 | + - ## Quick mental model |
| 71 | + - **`async { ... }`** → "Create a future value." |
| 72 | + - **`.await`** → "Extract the future's output; pause until ready." |
| 73 | + - **`block_on(fut)`** → "Drive the future to completion from sync code." |
| 74 | + - You can think of `.await` like Python `await`, **but it is not a function or property — it is language syntax.** |
0 commit comments