We mentioned

Long time no update
rustRelated content up, update a wave
RustThis article will introduce the content of
RustComplex data types in the.

Composite Type

A compound datatype is a data type that can be composed of a primitive datatype and other compound types to form a compound type action, also known as composition.

This article introduces the complex types of tuple, array, struct, and enum in Rust.

tuple

A tuple type is a composite type composed of several elements of different types. The elements are grouped together into a new data type by () braces. The length of a tuple is fixed at the time of definition and cannot be changed. If the data type of the element is specified, then your element must be specified. Otherwise the compiler will teach you a lesson!

Example:

Fn main() {let tup_type:(i8,i32,bool) = (21,-1024,true); // Deconstruct the element let (one,two,three) = tup_type; // let tup_2d:(f64,(i8,i32,bool)) = (3.1415927,(one,two,three)); println! ("tup_2d = {:? }",tup_2d); / / println index! (" PI = {:? }",tup_2d.0); }

Tuples can be accessed in several ways. They can be accessed by subscripting or by using deconstruction to assign new variables to access them, but iterators are not supported.

for v in tup_2d.1.iter() { println! ("{}",v) }
Compiling playground v0.0.1 (/playground) error[E0599]: Compiling playground v0.0.1 (/playground) error[E0599]: no method named `iter` found for tuple `(i8, i32, bool)` in the current scope --> src/main.rs:10:23 | 10 | for v in tup_type.iter() { | ^^^^ method not found in `(i8, i32, bool)` error: aborting due to previous error For more information about this error, try `rustc --explain E0599`. error: could not compile `playground` To learn more, run the command again with --verbose.

Each element of a tuple can have a different type, so you cannot iterate over it. Tuples are not even guaranteed to store data in the same order as the type definition, so even if you implement Iterators for them yourself, they are not suitable for efficient iteration.

However, if the element is supported and implements an Iterator, it can be iterated through.iter().

let mut arrays:[usize;5] = [0;5]; for i in 0.. 5 { arrays[i] = i+1; } println! (" {:? }",arrays); let tup_arr:(&str,[usize;5]) = ("tup_arr",arrays); println! (" {:? }",tup_arr); for v in tup_arr.1.iter() { println! ("{}",v) }

For example, the element in Rust is an array. Arrays in Rust, like any other language, are a composite type of a group of elements of the same type. Arrays are a contiguous memory space in the underlying storage.

array

The array declarations in Rust are made by [T;n], where T is the element type and n is the number of pits in the group of elements. When created, you can remove the type and size and the program will infer this automatically.

// Array let arr:[f32;3] = [1.0,2.2,3.33]; println! (" {:? }",arr); Let arr_infer = ["Hello",",","World!"] ; let mut str = String::new(); // Iterator for v in arr_infer. Iter () {STR. Push_str (v); } println! ("str = {}",str);

Click to see the tuple code example

enum

Enumeration types, which are familiar to those of you who have worked in Java before, are also available in Rust. An enumeration type is a custom data type, declared using the enum keyword. The body can contain multiple custom enumeration values.

    #[derive(Debug)]
    enum Gender {
        Boy,
        Girl,
    }

There is an enumeration of type Gender defined above. Boy and Girl are the values available for the enumeration. #[Derive (Debug)] allows Gender to automatically Debug the Tarit (later in this article).

struct

Structs can assemble custom data types from existing types into new custom data types. You can create a structure using the struct keyword. The structure field format is name:type, name is the name of the structure field, type is the type of the field.

#[Derive (Debug)] enum Gender {Boy, Girl, #[Derive (Debug)] struct Programmer<'skill {name: String, skill: [&'skill STR; 3], sex: Gender,} // create an instance let engineer = Programmer {name: String::from("Jaco Ding"), // variable skill: [" Java "and" Go ", "Rust"], / / a string of length 3 face type of the array sex: Gender: : Boy, / / by enumeration limit parameter type}; println! ("engineer = {:? }",engineer); }

With custom types, structs can be configured to handle specific requirements. For example, this code defines an array with an element of type Programmer of length 2.

let Doris = Programmer { name: String::from("Doris"), skill: ["Vue","TypeScript","JavaScript"], sex:Gender::Girl, }; let Jaco = Programmer { name: String::from("Jaco"), skill: ["Java","Go","Rust"], sex:Gender::Boy, }; let employees:[Programmer;2] = [Doris,Jaco]; for e in employees.iter() { println! (" {:? }",e) }

<‘skill> in structural Programmer resolves the life cycle of skill arrays undeclared lifetime, ownership, which is at the heart of Rust and will be updated in later articles.

summary

There are also two special structures for structures in Rust: tuple structures, single structures, and enums, as well as enumerations with parameters… This article summarizes the use of common compound types, but does not go into depth.