1
1
# Arrays and Slices
2
2
3
3
An array is a collection of objects of the same type ` T ` , stored in contiguous
4
- memory. Arrays are created using brackets ` [] ` , and their size , which is known
5
- at compile time, is part of their type signature ` [T; size ] ` .
4
+ memory. Arrays are created using brackets ` [] ` , and their length , which is known
5
+ at compile time, is part of their type signature ` [T; length ] ` .
6
6
7
- Slices are similar to arrays, but their size is not known at compile time.
7
+ Slices are similar to arrays, but their length is not known at compile time.
8
8
Instead, a slice is a two-word object, the first word is a pointer to the data,
9
9
and the second word is the length of the slice. The word size is the same as
10
10
usize, determined by the processor architecture eg 64 bits on an x86-64.
@@ -31,8 +31,8 @@ fn main() {
31
31
println!("first element of the array: {}", xs[0]);
32
32
println!("second element of the array: {}", xs[1]);
33
33
34
- // `len` returns the size of the array
35
- println!("array size : {}", xs.len());
34
+ // `len` returns the count of elements in the array
35
+ println!("number of elements in array : {}", xs.len());
36
36
37
37
// Arrays are stack allocated
38
38
println!("array occupies {} bytes", mem::size_of_val(&xs));
0 commit comments