谁在掌控你的 Rust 未来?Rust 异步编程动手入门
Who Runs Your Rust Future? Hands-On Intro to Async Rust

原始链接: https://aibodh.com/posts/async-rust-chapter-1-hands-on-intro-to-async-rust/

本系列旨在架起一座桥梁,帮助读者从理解 Rust 异步模型的内部机制,过渡到利用 Tokio 开发实际应用。JavaScript 通过隐藏的事件循环来管理异步,而 Rust 则有意地保持“懒惰”:异步函数返回的 `Future` 在被显式轮询之前不会执行任何操作。 作者认为,不亲手构建异步引擎,就无法真正掌握 Rust 的异步编程。本系列将异步拆解为三个核心组件: * **Future:** 一个表示尚未完成工作的状态机。 * **Polling(轮询):** 通过“触发” Future 来检查其能否继续运行,或是必须返回 `Pending` 并等待。 * **Waker:** 一种通知执行器在可以取得进展时重新进行轮询的机制。 通过实现这些组件,以及 `oneshot` 通道和基础的 `block_on` 运行器,作者揭开了 `Pin`、`Context` 和线程挂起(thread parking)等概念的神秘面纱。这种实践性的学习方式确保了读者在最终使用 Tokio 等高级库时,能够深刻理解其底层运作原理。本系列优先强调深度理解而非捷径,助力开发者从单纯的“使用”异步编程转向精通其实现细节。

Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 谁在掌控你的 Rust 未来?异步 Rust 实战入门 (aibodh.com) 3 分 | febin | 1 小时前 | 隐藏 | 过往 | 收藏 | 讨论 | 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
On AI assistance
"Bodh" in "AIBodh" means understanding. Since conversational AI first arrived, the main thing I kept coming back to it for was learning, asking, getting it wrong, asking again, until something finally made sense. That turned into a belief I still hold: used well, AI can genuinely help people learn. So I use it for teaching, but not as a shortcut. Each article still takes me 20 to 25 hours. AI is how I get through the parts of writing that are genuinely hard: finding the example where a concept is the obvious answer, the analogy that clicks, the visual that makes it concrete, the pass that cuts the jargon and the bloat. I verify the code runs, I keep the voice mine, and I cut anything that does not help you understand. The judgment and the corrections stay with me. If anything feels off in any section, let me know on Discord and I'll work on it.

Async Rust is usually taught from one of two directions. The async book and the runtime write-ups show you the internals: how Future, poll, and Pin work, and you can even hand-build a tiny executor. Tokio’s guides go the other way, you wire up a real service and it runs. Both are genuinely useful. What is harder to find is the bridge between them, the part that connects understanding how async works to actually shipping with it.

That bridge is what this series is. You build the engine yourself, the future, the waker, the executor, until you understand every piece, and then you drive the real one, Tokio, and recognize those same pieces because you built them.

Couples therapy Couples therapy
Couples therapy

This series assumes two things. First, that you’ve written async and await code in JavaScript before. Second, that you’re comfortable with Rust’s basics: structs, enums, associated functions, and closures (all covered in the free chapters of The Impatient Programmer’s Guide to Bevy and Rust).

A quick note on the series: I plan to keep at least 5 to 8 chapters free to read here, with the rest collected in a paid ebook. The free chapters may not follow the numbering in order, so some will land out of sequence.

Future

If you’ve shipped any modern JavaScript, you’ve done this a hundred times:

Pseudocode, don't use.

async function getUser() { /* ... */ }const user = await getUser();   // and it just... works

You write async function, add await, and it runs. You never had to think about what runs it, because something always did. The node process ships a built-in event loop: a hidden engine that picks up your Promises and pushes each one forward until it’s done.

Node's event loop is a cycle that never stops. On every spin it grabs a Promise that can move forward, runs it until it has to wait, then sets it aside and picks up the next one. Node keeps the loop turning for you. Node's event loop is a cycle that never stops. On every spin it grabs a Promise that can move forward, runs it until it has to wait, then sets it aside and picks up the next one. Node keeps the loop turning for you.

In JavaScript you never had to ask who keeps that loop running. But what about Rust? Who actually runs your async code? Or, in Rust’s own terms: who runs your future?

Rust does the opposite, on purpose. It ships no event loop at all. Nothing in the language is sitting around waiting to run your async code. If you want one, you bring it in. Or, the way we’re going to learn it, you build one yourself.

A second job A second job
A second job

That sounds like a missing feature. By the end of this series it’ll feel like the opposite. But before we ask who runs it, let’s be precise about what async is.

A normal function is the kind you write every day. You call it, it runs then and there, and by the next line its return value is already sitting in your variable: