Rust Struct
In Rust, structs and tuples can bundle together several pieces of data of possibly different types into a single whole, but each member of a struct and the struct itself have names, so you don't have to remember indices when accessing members. Tuples are often used for undefined multi-value passing, while structs are used for standard commonly-used data structures. Each member of a struct is called a "field".
### Struct Definition
This is a struct definition:
```rust
struct Site { domain: String, name: String, nation: String, found: u32 }
Note: If you frequently use C/C++, please remember that in Rust, the struct statement is only used for definition, not for declaring instances, there is no ; symbol at the end, and each field definition is separated by ,.
### Struct Instance
Rust is influenced by JavaScript in many ways. When instantiating a struct, you use the JSON object's key: value syntax to define it:
## Example
```rust
let tutorial=Site{
domain:String::from("www."),
name:String::from("TUTORIAL"),
nation:String::from("China"),
found:2013
};
If you are not familiar with JSON objects, you can ignore it, just remember the format:
StructName { field_name : field_value, ...}
The benefit is that it not only makes the program more intuitive, but also allows you to input member values without following the order of the definition.
If the struct instance has field names that are the same as existing variable names, you can simplify the writing:
## Example
```rust
let domain=String::from("www.");
let name=String::from("TUTORIAL");
let tutorial=Site{
domain,// same as domain : domain,
name,// same as name : name,
nation:String::from("China"),
traffic:2013
};
There is a situation: you want to create a new struct instance where most properties need to be set to the same as an existing struct's properties, only changing one or two field values. You can use the struct update syntax:
```rust
let site=Site{domain:String::from("www."),name:String::from("TUTORIAL"),..tutorial };
Note: There cannot be a comma after ..tutorial. This syntax does not allow copying another struct instance unchanged, meaning you must reset at least one field's value to reference other instance values.
### Tuple Struct
There is a simpler way to define and use structs: **Tuple Struct**.
A tuple struct is a struct that takes the form of a tuple.
The difference from tuples is that it has a name and a fixed type format. Its purpose is to handle simple data that needs type definition (frequently used) but doesn't want to be too complex:
```rust
struct Color(u8, u8, u8);
struct Point(f64, f64);
let black = Color(0, 0, 0);
let origin = Point(0.0, 0.0);
"Color" and "point coordinates" are two commonly used data types, but if you write a big bracket and then two names just for readability when instantiating, it sacrifices convenience. Rust doesn't leave this problem behind. Using tuple struct objects is the same as tuples, accessed through . and index:
## Example
```rust
fn main(){
struct Color(u8,u8,u8);
struct Point(f64,f64);
let black = Color(0,0,0);
let origin = Point(0.0,0.0);
println!("black = ({}, {}, {})", black.0, black.1, black.2);
println!("origin = ({}, {})", origin.0, origin.1);
}
Output:
black = (0, 0, 0) origin = (0, 0)
* * *
## Struct Ownership
Structs must own the field values, because when the struct is dropped, it releases all fields.
This is why the examples in this chapter use String type instead of &str.
But this doesn't mean you can't define reference-type fields in structs. This requires the "lifetime" mechanism.
But it's difficult to explain the "lifetime" concept now, so it can only be explained in later chapters.
### Output Struct
In debugging, completely displaying a struct instance is very useful. But if we manually write a format, it would be very inconvenient. So Rust provides a convenient way to output an entire struct:
## Example
```rust
#[derive(Debug)]
struct Rectangle {
width:u32,
height:u32,
}
fn main(){
let rect1 = Rectangle { width:30, height:50};
println!("rect1 is {:?}", rect1);
}
As shown in the first line: you must import the debug library **#[derive(Debug)]**, then you can use the {:?} placeholder in println and print macros to output an entire struct:
rect1 is Rectangle { width: 30, height: 50 }
If there are many properties, you can use
YouTip