Rewrite structs

pull/67/head
Dhghomon 4 years ago committed by GitHub
parent 716aa42b46
commit 91a7e20df1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2327,13 +2327,13 @@ Looks like a normal number
## Structs
With structs, you can create your own type. Structs are created with the keyword `struct`. The name of a struct should be in UpperCamelCase (capital letter for each word, no spaces).
With structs, you can create your own type. You will use structs all the time in Rust because they are so convenient. Structs are created with the keyword `struct`. The name of a struct should be in UpperCamelCase (capital letter for each word, no spaces). If you write a struct in all lowercase, the compiler will tell you.
There are three types of structs. One is a "unit struct". Unit means "doesn't have anything".
There are three types of structs. One is a "unit struct". Unit means "doesn't have anything". For a unit struct, you just write the name and a semicolon.
```rust
struct FileDirectory;
fn main() { }
fn main() {}
```
The next is a tuple struct, or an unnamed struct. It is "unnamed" because you only need to write the types, not the variable names. Tuple structs are good when you need a simple struct and don't need to remember names.
@ -2347,7 +2347,9 @@ fn main() {
}
```
The third type is the named struct. This is probably the most common struct. In this struct you declare variable names and types inside a `{}` code block.
This prints `The second part of the colour is: 0`.
The third type is the named struct. This is probably the most common struct. In this struct you declare variable names and types inside a `{}` code block. Note that you don't write a semicolon after a named struct, because there is a whole code block after it.
```rust
struct Colour(u8, u8, u8); // Declare the same Colour tuple struct
@ -2367,7 +2369,7 @@ fn main() {
}
```
In a named struct, you separate variables by commas. For the last variable you can add a comma or not - it's up to you. `SizeAndColour` had a comma after `colour`:
You separate variables by commas in a named struct too. For the last variable you can add a comma or not - it's up to you. `SizeAndColour` had a comma after `colour`:
```rust
struct Colour(u8, u8, u8); // Declare the same Colour tuple struct
@ -2377,7 +2379,7 @@ struct SizeAndColour {
colour: Colour, // And we put it in our new named struct
}
fn main() { }
fn main() {}
```
but you don't need it. But it can be a good idea to always put a comma, because sometimes you will change the order of the variables:
@ -2390,7 +2392,7 @@ struct SizeAndColour {
colour: Colour // No comma here
}
fn main() { }
fn main() {}
```
Then we decide to change the order...
@ -2401,11 +2403,12 @@ struct SizeAndColour {
size: u32,
}
fn main() { }
fn main() {}
```
But it is not very important either way so you can choose whether to use a comma or not.
Let's create a `Country` struct to give an example. The `Country` struct has the fields `population`, `capital`, and `leader_name`.
```rust
@ -2417,7 +2420,7 @@ struct Country {
fn main() {
let population = 500_000;
let capital = String::from("Elist");
let capital = String::from("Elista");
let leader_name = String::from("Batu Khasikov");
let kalmykia = Country {
@ -2428,7 +2431,7 @@ fn main() {
}
```
Did you notice that we wrote the same thing twice? Actually, you don't need to do that. If the field name and variable name are the same, you don't have to write it twice.
Did you notice that we wrote the same thing twice? We wrote `population: population`, `capital: capital`, and `leader_name: leader_name`. Actually, you don't need to do that. If the field name and variable name are the same, you don't have to write it twice.
```rust
struct Country {
@ -2439,7 +2442,7 @@ struct Country {
fn main() {
let population = 500_000;
let capital = String::from("Elist");
let capital = String::from("Elista");
let leader_name = String::from("Batu Khasikov");
let kalmykia = Country {

Loading…
Cancel
Save