This is the fifth day of my participation in the August Wen Challenge.More challenges in August

An array of

An array is introduced

An array is a container that stores a series of data of the same type in contiguous memory and the size of the elements in the array has to be determined at compile time and the number of elements in the array itself has to be determined at compile time and immutable at execution time if you want to use a variable length container, The Vec/LinkedList array type in the library can be represented as [T;m], where T stands for element type; N represents the number of elements; N must be a constant integer whose size can be determined at compile time; Separate them with a semicolon.

    let arr = [1.2.3.4.5];
    let arr_1 = [1;3];
    let arr_2:[&str;5];
Copy the code

An array type

In Rust, only arrays with the same number of elements and element types are arrays of the same type. There is no implicit conversion between arrays and Pointers, so only arrays of the same type can be assigned to each other.

An iterator for an array

For the convenience of arrays, we usually need to use a for loop to iterate over an array. Rust’s array itself does not implement an iterable trait, but we can use array slicing, which implements the iterator trait, which helps us write simple code to iterate over arrays.

let arr = [1.2.3.4.5];
for i in &arr{
    println!("{:? }", i);
}
Copy the code
Multidimensional array

The rust multidimensional array is defined as follows:

    let arr: [[i32;3];3] = [[1.1.1], [1.1.1], [1.1.1]].for _i in &arr{
        / /...
    }
Copy the code

What is array slicing

Implementing the BORROW operation on an array yields a: Arrays section (slice), the ownership of the slice is not an array, the array slice actually equivalent to a pointer to an array, the compiler through internal operation allows us to get to the array slice by & operation types, array slice actually or pointer type, but to give up the length of the array in the compile phase save information, Instead, change the length information to a value for the run time.

DST and the fat pointer

The difference between array slice and array above shows that array slice is not only a pointer to array, but also carries the length information of array. It has a vivid name called “fat pointer”, and the corresponding concept of “fat pointer” is “dynamic size type” (DST).

println!("{:? }",std::mem::size_of::<&[i32;3> ());println!("{:? }",std::mem::size_of::<&[i32> ());/ / 8
/ / 16
Copy the code

For DST, Rust has the following restrictions:

  • It can only be created and manipulated indirectly through PointersDSTtype
  • Local variables and function arguments cannot beDSTType,
  • Enum Cannot containDST.structOnly the last element can be DST. All this seems to berustforDSTIs designed to makerustThe type system is much richer, and we can implement it using DSTtrait, used as generic parameters, etc. The design of fat pointer avoids the problem of automatically degenerate to bare pointer and length loss when the array is passed as parameter, thus ensuring the safety of the type.