# Using Structs to Structure Related Data

Structs and enums are the building blocks for ccrating new types in your program’s domain to take full advantage of Rust’s compile time type checking.

## Defining and Instantiating Structs

### Creating Instances From Other Instances With Struct Update Syntax

Note that the entire instance must be mutable.

### Unit-Like Structs Without Any Fields

unit-like structs — similar to ().

Unit-like structs can be useful in situations in which you need to implement a trait on some type but don’t have any data that you want to store in the type itself.

Ownership of Struct Data

Lifetime, can’t store &str(slice), only String.

In Chapter 10, we’ll discuss how to fix these errors so you can store references in structs, but for now, we’ll fix errors like these using owned types like String instead of references like &str.

## Method Syntax

Methods与functions异同：

To define the function within the context of Rectangle, we start an impl (implementation) block.

Where’s the -> Operator?

Rust doesn’t have an equivalent to the -> operator; instead, Rust has a feature called automatic referencing and dereferencing. Calling methods is one of the few places in Rust that has this behavior.

Here’s how it works: when you call a method with object.something(), Rust automatically adds in &, &mut, or * so object matches the signature of the method. In other words, the following are the same:

### Associated Functions

Another useful feature of impl blocks is that we’re allowed to define functions within impl blocks that don’t take self as a parameter. These are called associated functions because they’re associated with the struct. They’re still functions, not methods, because they don’t have an instance of the struct to work with. You’ve already used the String::from associated function.

Associated functions are often used for constructors.