知识卡片
元组结构体保留类型名
元组结构体为整体建立独立名称,字段仍以.0、.1等位置访问。例如TextMatch(usize,String)可把位置与文本组成一个有名字的值。它比裸元组多了类型身份,却没有字段名;若位置含义不清,应考虑具名结构体。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Aggregate Types · 查看原书依据
struct TextMatch(usize, String); // Construct by providing the contents in order. let m = TextMatch(12, "needle".to_owned()); // Access by field number. assert_eq!(m.0, 12);
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Aggregate Types》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u009-ch01-005)。
- 结论依据:原文在“struct TextMatch(usize, String);”附近的定义、例子或代码支撑本卡核心说明:元组结构体为整体建立独立名称,字段仍以.0、.1等位置访问。边界按已读前提解释;示例未执行。
- 原始内容:struct TextMatch(usize, String); // Construct by providing the contents in order. let m = TextMatch(12, "needle".to_owned()); // Access by field number. assert_eq!(m.0, 12);