This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Introduction to compound types

Compound types can put multiple values in a single type.

Rust provides two basic conformance types, tuples and arrays.

Tuples (Tuple)

Tuples can group multiple values of multiple types together. Note that the length of a tuple is fixed and cannot be changed once declared.

Tuples are created the same way as Python, with values separated by commas in parentheses, each position in a tuple corresponding to a type, and each element in a tuple need not be of the same type. Tupl. N allows access to elements with index N. It is also possible to use pattern matching to destructure a tuple to obtain the values of elements.

If only one element is stored in the tuple, the last comma must not be omitted.

fn main() {
    let tup: (bool.i32.usize.f64.char) = (true.128.256.6.2.'h');  // The standard way to define tuples, annotating the type of each element
    let (x, y, z, a, b) = tup;
    println!("{}, {}, {}, {}, {}", x, y, z, a, b);
    println!("{}, {}, {}, {}, {}", tup.0, tup.1, tup.2, tup.3, tup.4)}Copy the code

Note that the index to access the tuple element must be a value that can be determined at compile time, not a variable.

fn main() {
    let a = (1.2.0);
    let b = 1;
    println!("{}", a.b)  // TypeError will occur
}
Copy the code

Tuples are often used as simple combinations of data, such as an associated set of information that can be stored in a single tuple.

fn main() {
    // Indicates a person's name and age
    let name = "rust";
    let age = 10;
    println!("{}, {}", name, age);
    // Instead of storing associated data in multiple variables, store it in a single data structure
    let info = ("rust".10);  // Store &str and i32 at the same time
    println!("{}, {}", info.0, info.1);
}
Copy the code

Array (Array)

Arrays can also have multiple values in one type. Each element in an array must be of the same type, and the length of the array is fixed. Once declared, the length of the array is immutable.

When declaring arrays, as in other languages, each value is separated by a comma inside brackets. Similarly, if there is only one element in the array, it is recommended not to omit the last comma.

When declaring an array, you can specify the type of the element in the array and the length of the array. The expression is [type; n, where type indicates the type of the element in the array and n indicates the length of the array.

A special case of declaring an array is that if each element in the array has the same value, then you can specify the initial value in brackets, followed by one; And finally, the length of the array.

If you want to access the elements of an array, you can use an index. If you access an index beyond the range of the array, Rust may report an error at compile time, or it may pass. The reason is that Rust does a simple index check at compile time, but it always reports an error at run time.

fn main() {
    let a: [i32; 3] = [1.2.3]; // Annotate the type of array
    let b = [5; 3];  // let b = [5, 5, 5]
    let a_first = a[0];  // Use the index to access the element values of array A
    println!("{}", a_first);
    
    // Discuss index overflow
    let index = 4;
    let val_a = a[index];  // Both compile and run will report an error, this is the simple case
    
    let val_a_complex = a[b[0]].// The code is slightly more complex, but it will compile without error, but will run with an error
}
Copy the code

Arrays are handy if you want data to be stored on the stack rather than the heap, or if you want to have a fixed number of elements. A Vector is a similar data structure in Rust, but a Vector is more flexible and can vary in length. This type will be covered in a later article.

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)