basic/compound-type/array #659
Replies: 24 comments 50 replies
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
在实际开发中,使用最多的是数组切片[T],我们往往通过引用的方式去使用&[T],因为后者有固定的类型大小 |
Beta Was this translation helpful? Give feedback.
-
“- 数组类型容易跟数组切片混淆,[T;n]描述了一个数组的类型,而[T]描述了切片的类型, 因为切片是运行期的数据结构,它的长度无法在编译期得知,因此不能用[T;n]的形式去描述
|
Beta Was this translation helpful? Give feedback.
-
数组是 复合类型 吧 |
Beta Was this translation helpful? Give feedback.
-
哎呀,越学越迷糊,可能是我TS用多了,总觉得这也不能动,那也不能动,动不动所有权就丢了,就很迷茫。就像是开拖拉机的进了飞机驾驶舱,手都不知道该放哪里 |
Beta Was this translation helpful? Give feedback.
-
如果是结构体数组也是存储在栈上吗? |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let a = [1, 2, 3];
let b = &(a[0..1]);
println!("{:?}", b);
let tmp = a[0..1];
let b = &(tmp);
} 第 4 行的 |
Beta Was this translation helpful? Give feedback.
-
为什么第一个 |
Beta Was this translation helpful? Give feedback.
-
有个疑问,为什么 &[T] 是 [T;n] 类型? |
Beta Was this translation helpful? Give feedback.
-
切片引用的&[T]既然是固定长度,为啥不体现在类型中呢 |
Beta Was this translation helpful? Give feedback.
-
Update: rust-lang/rust#89379 |
Beta Was this translation helpful? Give feedback.
-
最后的总结的最后两个循环直接用 for i in a就可以了吧? |
Beta Was this translation helpful? Give feedback.
-
切片类型[T]拥有不固定的大小,而切片引用类型&[T]则具有固定的大小,因为 Rust 很多时候都需要固定大小数据类型,因此&[T]更有用,&str字符串切片也同理 1.切片类型为什么是[T], 切片不都是引用吗? |
Beta Was this translation helpful? Give feedback.
-
vector没有讲么 |
Beta Was this translation helpful? Give feedback.
-
为什么 |
Beta Was this translation helpful? Give feedback.
-
这句话我想了好半天才反应过来,切片 |
Beta Was this translation helpful? Give feedback.
-
std::array::from_fn(|_i| your_item_cstor) |
Beta Was this translation helpful? Give feedback.
-
let array = [String::from("rust is good!"),String::from("rust is good!"),String::from("rust is good!")];
let array: [String; 8] = std::array::from_fn(|_i| String::from("rust is good!"));
很难评这两个谁更难看 |
Beta Was this translation helpful? Give feedback.
-
fn func(mut a: [i32; 6]) { fn main() { |
Beta Was this translation helpful? Give feedback.
-
“因为后者有固定的类型大小”,感觉可以翻译为:“因为数组切片[T]有固定的类型大小” “后者” 在这里有点突兀 |
Beta Was this translation helpful? Give feedback.
-
let array = std::array::from_fn::<_, 5, _>(|index| index.to_string()); 这样写是不是更完美 |
Beta Was this translation helpful? Give feedback.
-
我们也迈出了坚定的第一步! |
Beta Was this translation helpful? Give feedback.
-
感谢大佬的文档,我稍微总结了一下: 为什么 option比空值好?(Option的三大特性) 明确性 强制性 可读性 |
Beta Was this translation helpful? Give feedback.
-
basic/compound-type/array
https://course.rs/basic/compound-type/array.html
Beta Was this translation helpful? Give feedback.
All reactions