RUST Learning Diary Lesson 10 – Compound data types
0 x00 review
In the last video we looked at println in general, but we’re going to continue with data types, and it’s been a long time since we looked at basic data types. Ha ha, can’t wait. Compound Types generally refer to Types that are a combination of other data Types. Rust’s original composite data types are arrays and tuples.
0x01 Array (Array)
An array is a composite type composed of elements of the same type. The length of an array is fixed and cannot grow or shrink once declared.
The array declaration
The type signature of an array in Rust is: [T;N]. T represents the data type and N represents the length of the array, which must be determined at compile time. Arrays can be initialized in two ways:
1. All values are listed in brackets, separated by commas. Example code is as follows:
// The type is not declared
let a = [1.2.3.4.5];
// Declaration type
let b: [i32; 5] = [1.2.3.4.5];
Copy the code
2. In parentheses, the first value represents the default initial element, the second value represents the length, and the semicolon is used in the middle. Separated. Example code is as follows:
// The type is not declared
let c = [2; 5];
// Declaration type
let d: [i32; 5] = [2; 5];
Copy the code
The above code says initialize an array of length 5 with an initial value of 2 for each element.
Array access
Elements in Rust that respond to positions in an array can be accessed by the array name [index]. Index values are counted from 0. Example code is as follows:
let e = [2.4.6.8.10];
println!("The element in the third position of array E is {}", e[2])
Copy the code
Code execution result:
The third element in e is zero6
Copy the code
An array
Because the length Of the array is fixed, an Index that exceeds the length Of the array will result in an Index Out Of Bounds. The length of the array can be obtained using len(). The following code outputs a length of 5:
let f = [1.3.5.7.9];
println!("The length of the array f is {}", f.len());
// The following code will have data out of bounds
println!("{}", f[5]);
Copy the code
The above code executes println! (“{}”, f[5]); The array is out of bounds, and the following error message is reported. Although Rust is a secure language, data transgressions are inevitable.
0x02 Tuple
A tuple is a compound type composed of elements of one or more types. No official definition was given. In use, tuples usually contain at least two elements. Of course, a tuple can contain only one element, or no element.
Tuples statement
A tuple is a heterogeneous finite sequence. Heterogeneous means that elements within a tuple can be of different types. Finite means that the length of a tuple is fixed. The signature format of a tuple is :(T,U,M), where T,U, and M indicate the type. Example code is as follows:
let tup: (i32.f64.bool) = (20.3.14.false);
// A tuple of an element
let tup_single: (i32= (,)5,);// The following two ways are not a tuple
// They are equivalent to let tup_wrong = 5;
// let tup_wrong = (5);
// let tup_wrong: (i32) = (5);dbg! (tup_single);/ / empty tuple
letempty_tup = (); dbg! (empty_tup);Copy the code
In the above code, if you want to declare a tuple with only one element, you need to put an English comma after the element. Let tup_wrong = (5) This is not a tuple, it’s equivalent to let tup_wrong = 5. The empty tuple is denoted by () and does not take up any memory space.
Tuples index
Tuples default by using the tuple name. Index “to access the element at the corresponding position in the tuple. Example code is as follows:
let tup : (i32.f64.bool) = (20.3.14.false);
println!("{} {} {}", tup.0, tup.1, tup.2);
Copy the code
Tuple deconstruction
A tuple is a single composite element, which can be destructured using Pattern Matching in addition to obtaining a value within the tuple by indexing. The **let + pattern ** is usually used for deconstructing tuples. Example code is as follows:
let tup : (i32.f64.bool) = (20.3.14.false);
let (x, y, z) = tup;
println!("x: {}, y: {}, z: {}", x, y, z);
Copy the code
Code execution result:
x: 20, y: 3.14, z: false
Copy the code
First create a tuple tUP variable, then use let+ pattern to divide TUP into three variables X, y, and z. This process is called Destructure of tuples.
In addition to binding variables, PS: let also supports pattern matching. Pattern matching will be covered in a later chapter.
0 x03 summary
This lesson learned about Rust’s primitive arrays and tuples of composite data types. We’ve had a brief introduction to arrays in this video, and we’ll go into more detail in the next video on vectors. You may be browsing through other articles and find compound data types and structures, enumerations, etc., more on that later. Arrays and tuples in this lesson, and scalar data types in Lesson 6 — integers, floats, Booleans, and characters. ** The values of these data types are stored directly in the “stack”. ** They have no concept of ownership. Ownership is one of Rust’s key knowledge points. The knowledge of stacks and ownership and weights will also be covered in subsequent chapters.
0x04 This section source code
010 · StudyRust – Code Cloud – Open Source China (gitee.com)
Next, learn about Rust vectors (dynamic arrays).