← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 10882 · 生成模型:未记录

元组按字段位置组合值

理解等级:中级

元组以固定位置组合若干字段,各字段可以有不同类型。(i32, bool) 的字段用 t.0 与 t.1 访问,字段位置写在语法里。数组的同类型元素才适合用变量索引。当函数用元组返回多项结果时,可写 let (head, tail) = text.split_at(21) 一次解构,并给各项命名。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第02章《Fundamental Types》|用元组表达一组值与空结果,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch02.xhtml#tuples` - 结论依据:原文比较元组字段和数组索引,随后以字符串切分说明用解构接收两个返回值。 - 原始内容:arrays. For one thing, each field of a tuple can have a different type, whereas an array’s elements must all be the same