← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.c.11 · 生成模型:未记录

元组结构体区分领域类型

理解等级:中级

元组结构体有独立类型名,字段可按位置访问。即使 Color 与 Point 有相同的三整数表示,函数参数仍能区分颜色和位置。无字段的单元结构体可作为实现 trait 的空载体。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 用结构体组织相关数据 Defining and Instantiating Structs · 查看原书依据

      Note that the black and origin values are different types because they’re instances of different tuple structs. Each struct you define is its own type, even though the fields within the struct might have the same types. For example, a function that takes a parameter of type Color cannot take a Point as an argument, even though both types are made up of three i32 values.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第05章 用结构体组织相关数据(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c05.xhtml`,块 0046) - 结论依据:Point 和 Color 都装三个 i32,书中以参数类型展示彼此不可替代。 - 原始内容:Note that the black and origin values are different types because they’re instances of different tuple structs. Each struct you define is its own type, even though the fields within the struct might have the same types. For example, a function that takes a parameter of type Color cannot take a Point as an argument, even though both types are made up of three i32 values.