A `struct` creates a Rust type that groups related values. Think of a country with a population, capital, and leader: those properties belong together. Use an `enum` instead when you need to select one of several choices. section 4.1 section 4.1.1
Rust has three forms of struct:
- A unit struct has no fields: `struct FileDirectory;` section 4.1.1
- A tuple struct has values without field names. Access them by position, such as `my_color.1`. section 4.1.1
- A named struct lists field names and types inside braces. This is the most common form. section 4.1.1
struct Country {
population: u32,
capital: String,
leader_name: String
}
fn main() {
let population = 500_000;
let capital = String::from("Elista");
let leader_name = String::from("Batu Khasikov");
let kalmykia = Country {
population,
capital,
leader_name,
};
}
The `Country` declaration gives each field a name and type. Creating a `Country` requires a value for every field. When a variable already has the same name as its field, you can write `population` rather than `population: population`. section 4.1.1
Name struct types in `UpperCamelCase`; the compiler warns if you use lowercase instead. You can then give a struct behavior with an `impl` block. A method uses `self` in some form and is usually called with a dot. An associated function does not take `self` and is called with `::`; it can create a new value. To print a struct with `{:?}`, add `#[derive(Debug)]` above its declaration. section 4.1.1 section 4.1.5
A struct for JSON data
A `CurrentArticle` struct groups the `title`, `description`, and `extract` fields needed from a JSON response. Deriving `Deserialize` lets `serde_json::from_str()` convert the response into that struct. section 23.3.2













