compound-types/array #186
Replies: 33 comments 3 replies
-
开头有个打错了: |
Beta Was this translation helpful? Give feedback.
-
1、数组的声明 Q: fn main() {
// 使用合适的类型填空
let arr: __ = [1, 2, 3, 4, 5];
// 修改以下代码,让它顺利运行
assert!(arr.len() == 4);
} A: fn main() {
// 使用合适的类型填空
let arr: [i32;5] = [1, 2, 3, 4, 5];
// 修改以下代码,让它顺利运行
assert!(arr.len() == 5);
} 2、 Q: fn main() {
// 很多时候,我们可以忽略数组的部分类型,也可以忽略全部类型,让编译器帮助我们推导
let arr0 = [1, 2, 3];
let arr: [_; 3] = ['a', 'b', 'c'];
// 填空
// 数组分配在栈上, `std::mem::size_of_val` 函数会返回整个数组占用的内存空间
// 数组中的每个 char 元素占用 4 字节的内存空间,因为在 Rust 中, char 是 Unicode 字符
assert!(std::mem::size_of_val(&arr) == __);
} A: fn main() {
// 很多时候,我们可以忽略数组的部分类型,也可以忽略全部类型,让编译器帮助我们推导
let _arr0 = [1, 2, 3];
let arr: [_; 3] = ['a', 'b', 'c'];
// 填空
// 数组分配在栈上, `std::mem::size_of_val` 函数会返回整个数组占用的内存空间
// 数组中的每个 char 元素占用 4 字节的内存空间,因为在 Rust 中, char 是 Unicode 字符
assert!(std::mem::size_of_val(&arr) == 12);//3*4
} 3、数组中所有值统一初始化 Q: fn main() {
// 填空
let list: [i32; 100] = __ ;
assert!(list[0] == 1);
assert!(list.len() == 100);
} A: fn main() {
// 填空
let list: [i32; 100] = [1;100] ;//从下文可知这里是需要初始化成为一个100个元素都为1的数组
assert!(list[0] == 1);
assert!(list.len() == 100);
} 4、 略(把 5、 略(把 6、 消除越界索引,把最后 |
Beta Was this translation helpful? Give feedback.
-
每天进步一点点,今日份作业done |
Beta Was this translation helpful? Give feedback.
-
Done. |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
done,下班打卡 |
Beta Was this translation helpful? Give feedback.
-
day3 完成了复合类型部分 |
Beta Was this translation helpful? Give feedback.
-
compound-types/array
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/compound-types/array.html
Beta Was this translation helpful? Give feedback.
All reactions