← 返回刚才阅读的位置

知识卡片

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

数组定长并完整初始化

理解等级:初级

定长数组写作 [T; N],N 是编译期常量。[1, 2, 4] 逐项给值,[0u8; 1024] 则把同一初值放到每个字节。数组长度属于类型,采用运行时才知道的 n 时,可改用 vec![0u8; n]。数组借给切片后,原来的元素仍可在切片方法中被处理。

关联阅读:[[向量的创建与遍历]]、[[用切片统一借用连续数据]]。

这张卡片看不懂?

已有反馈 · 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#arrays` - 结论依据:原文用重复值语法构造筛表和定长缓冲,再以运行时长度不能作为数组长度说明数组和向量的选型。 - 原始内容:An array’s length is part of its type and fixed at compile time. If n is a variable, you can’t write [true; n] to get