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

Tuple structure

In addition to defining the conventional structure, can also define structure similar to that of the tuple, known as the yuan group structure (tuple user-defined structs), the main characteristic of tuple structure is not the field name, field type only, when we want to come up with a name for the tuple, and make the yuan of RMB with other groups of different types, tuple structure is very useful, It becomes redundant and formalized to name each field as a regular structure would, such as defining the RGB and coordinates of the color. To define a tuple structure, start with the struct keyword and the structure name followed by the type in the tuple. For example, here is the definition and usage of two tuple structures called Color and Point respectively:

struct Color(i32.i32.i32);
struct Point(i32.i32.i32);

let black = Color(0.0.0);
let origin = Point(0.0.0);
Copy the code

Note that the black and Origin values are of different types because they are instances of different tuple structures. Each structure you define has its own type, even if the fields in the structure have the same type. For example, a function that takes a parameter of type Color cannot accept Point as an argument, even though both types consist of three i32 values. In other respects, instances of tuple structures are similar to tuples: they can be deconstructed into separate parts, or they can be used with. Followed by indexes to access individual values, and so on.

Element structure

We can also define a structure without any fields! They are called unit-like structs because they resemble (), the unit type. Class unit constructs often come into play when you want to implement traits on a type but don’t need to store data in the type. Traits will be introduced in a later article.

Ownership of structure data

struct UserInfo {
    name: String,
    age: i32,
    sex: bool.// true indicates male and false indicates female
}
Copy the code

In the above code example, the owner-string type is used instead of the &STR String slice type. The reason is that you want the structure to own all of its data, so as long as the whole structure is valid its data is valid. The concept of a lifetimes is mentioned here and will be introduced in future articles. A lifetimes ensures that the validity of the data referenced by the structure is consistent with the structure itself. Attempting to store a reference in a structure without specifying a lifetime will cause the program to fail to compile:

struct User {
    username: &str,
    email: &str,
    sign_in_count: u64,
    active: bool,}fn main() {
    let user1 = User {
        email: "[email protected]",
        username: "someusername123",
        active: true,
        sign_in_count: 1}; }Copy the code

The following error message is displayed:

error[E0106]: missing lifetime specifier
 -->
  |
2 |     username: &str,
  |               ^ expected lifetime parameter

error[E0106]: missing lifetime specifier
 -->
  |
3 |     email: &str,
  |            ^ expected lifetime parameter
Copy the code

We’ll show you how to fix this in a future article to store references in a structure.

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 (╹▽╹)