知识卡片
属性宏包装异步入口
异步函数提供可由运行时驱动的异步计算。Tokio 是 Rust 的异步运行时,#[tokio::main] 让调用者能以异步形式写程序入口,由宏生成进入运行时所需的包装。书中列出的宏入口接收 args 与 item 两个流,交给内部入口处理。属性路径里的 tokio:: 标明宏的来源,导入 tokio::main 后可直接写 #[main]。这一例说明属性宏的替换能力也可用来保留业务代码,同时把执行环境接到原函数上。
相关卡片 [[属性宏替换原项目]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Making fields public with attribute macros 框架中的属性宏与解析委托 · 查看原书依据
Lots of libraries use attribute macros to do cool things. For example, Tokio, a widely used asynchronous runtime for Rust, uses its #[tokio::main] macro to transform your main into something that can handle asynchronous calls. This is
参考来源
- 位置:《Write Powerful Rust Macros》第04章《Making fields public with attribute macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-4.xhtml#p112)
- 结论依据:第 4.8 节展示 #[tokio::main] 适配异步入口的用途、程序宏入口签名和显式导入写法,内部实现以书中源码版本为范围。
- 原始内容:Lots of libraries use attribute macros to do cool things. For example, Tokio, a widely used asynchronous runtime for Rust, uses its #[tokio::main] macro to transform your main into something that can handle asynchronous calls. This is