← 返回刚才阅读的位置

知识卡片

结构图卡 · 6074.d.1.a.3.c.4.e.3.a.2.c · 生成模型:GPT-6

AST 只能看见类型写法

理解等级:高级

过程宏可以从 Field.ty 匹配 Type::Path,再从该路径取出 PathSegment.ident。教学实现把第一段写法与 "String" 比较,以便为裸写的 String 生成特殊代码。限定路径可能以其他段开头,别名也有自己的标识符。没有匹配到该名字时,无法据此断定字段可隐式按值复制。用这种判断有意区分某种语法时,应明确说明接收的拼写形式。

相关卡片 [[消耗构建器以移动字段]]。

结构图:

classDiagram
    class Field {
        ty Type
    }
    class Type {
        <<enumeration>>
        Path(TypePath)
    }
    class TypePath {
        path Path
    }
    class Path {
        segments
    }
    class PathSegment {
        ident Ident
        arguments PathArguments
    }
    Field *-- Type : 字段类型
    Type *-- TypePath : Path 变体
    TypePath *-- Path : 包含路径
    Path *-- PathSegment : 路径段
这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第06章《Testing a builder macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-6.xhtml#p115) - 结论依据:图 6.4 与清单 6.23 展示从 Field.ty 到路径首段标识符的访问,随后 Vec<String> 的用例说明语法名判断无法建立 Copy 保证。 - 原始内容:That means we can write a helper function to retrieve the identifier, turn it into a string, and compare it with a string representation of the type. As you can see, writing this code is a matter of figuring out where to look for useful data.