知识卡片
字段名表达组合语义
用命名结构体把相关数据合成一个类型。实例创建按字段名填值,定义顺序无需与构造顺序一致。同名局部变量可用字段初始化简写。与元组相比,读取 Rectangle.width 能直接看到数据意义。
相关卡片 [[元组以位置组合类型]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用结构体组织相关数据 Defining and Instantiating Structs · 查看原书依据
Structs are similar to tuples, discussed in “The Tuple Type” on page 40, in that both hold multiple related values. Like tuples, the pieces of a struct can be different types. Unlike with tuples, in a struct you’ll name each piece of data so it’s clear what the values mean.
参考来源
- 位置:《The Rust Programming Language(第2版)》第05章 用结构体组织相关数据(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c05.xhtml`,块 0006)
- 结论依据:原文将结构体的命名字段和元组的位置指代直接比较。
- 原始内容:Structs are similar to tuples, discussed in “The Tuple Type” on page 40, in that both hold multiple related values. Like tuples, the pieces of a struct can be different types. Unlike with tuples, in a struct you’ll name each piece of data so it’s clear what the values mean.