← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.4.e.3.a.2.a.1.b · 生成模型:GPT-6

框架按语法类型读写

理解等级:中级

Rocket 是 Rust Web 框架,使用属性标记 HTTP 路由,例如 #[get("/add/endpoint")]。书中列出的 Rocket 源码片段展示框架中两种可复用的职责划分。输入侧,Invocation 的 Parse 实现读取不同语法节点,并在指定边界要求逗号。每次读取用 ? 上报解析失败,调用者得到解析错误。输出侧,UriExpr 的 ToTokens 识别当前是 URI 还是表达式,再委托对应节点写入同一个流。两段源码分别示范输入和输出的组织方式,各自可迁移到有多个语法形态的宏。

相关卡片 [[用解析特征定义输入语法]]、[[用字段模型委托输出]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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#p115) - 结论依据:第 4.8 节引入 Rocket 的端点属性,源码例对照 Invocation 的逐段解析与 UriExpr 的分支委托,原文指出 ? 比教学代码中的 unwrap 更适合报告失败。 - 原始内容:There are also lots of examples of libraries using the Parse and ToTokens traits. Here are some examples from Rocket, a web framework that uses a lot of macros for generating endpoints, among other things.