-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap some code in main to make scopes clearer. Fixes #2830.
- Loading branch information
1 parent
0afe66c
commit b9f7fbe
Showing
1 changed file
with
42 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,12 +55,14 @@ in that template with particular data to create values of the type. For | |
example, we can declare a particular user as shown in Listing 5-2. | ||
|
||
``` | ||
let user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
fn main() { | ||
let user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
} | ||
``` | ||
|
||
Listing 5-2: Creating an instance of the `User` struct | ||
|
@@ -72,14 +74,16 @@ the dot notation and assigning into a particular field. Listing 5-3 shows how | |
to change the value in the `email` field of a mutable `User` instance. | ||
|
||
``` | ||
let mut user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
fn main() { | ||
let mut user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
user1.email = String::from("[email protected]"); | ||
user1.email = String::from("[email protected]"); | ||
} | ||
``` | ||
|
||
Listing 5-3: Changing the value in the `email` field of a `User` instance | ||
|
@@ -150,12 +154,16 @@ regularly, without the update syntax. We set a new value for `email` but | |
otherwise use the same values from `user1` that we created in Listing 5-2. | ||
|
||
``` | ||
let user2 = User { | ||
active: user1.active, | ||
username: user1.username, | ||
email: String::from("[email protected]"), | ||
sign_in_count: user1.sign_in_count, | ||
}; | ||
fn main() { | ||
// --snip-- | ||
let user2 = User { | ||
active: user1.active, | ||
username: user1.username, | ||
email: String::from("[email protected]"), | ||
sign_in_count: user1.sign_in_count, | ||
}; | ||
} | ||
``` | ||
|
||
Listing 5-6: Creating a new `User` instance using one of the values from `user1` | ||
|
@@ -165,10 +173,14 @@ shown in Listing 5-7. The syntax `..` specifies that the remaining fields not | |
explicitly set should have the same value as the fields in the given instance. | ||
|
||
``` | ||
let user2 = User { | ||
email: String::from("[email protected]"), | ||
..user1 | ||
}; | ||
fn main() { | ||
// --snip-- | ||
let user2 = User { | ||
email: String::from("[email protected]"), | ||
..user1 | ||
}; | ||
} | ||
``` | ||
|
||
Listing 5-7: Using struct update syntax to set a new `email` value for a `User` | ||
|
@@ -210,8 +222,10 @@ two tuple structs named `Color` and `Point`: | |
struct Color(i32, i32, i32); | ||
struct Point(i32, i32, i32); | ||
let black = Color(0, 0, 0); | ||
let origin = Point(0, 0, 0); | ||
fn main() { | ||
let black = Color(0, 0, 0); | ||
let origin = Point(0, 0, 0); | ||
} | ||
``` | ||
|
||
Note that the `black` and `origin` values are different types, because they’re | ||
|
@@ -235,7 +249,9 @@ example of declaring and instantiating a unit struct named `AlwaysEqual`: | |
``` | ||
struct AlwaysEqual; | ||
let subject = AlwaysEqual; | ||
fn main() { | ||
let subject = AlwaysEqual; | ||
} | ||
``` | ||
|
||
To define `AlwaysEqual`, we use the `struct` keyword, the name we want, then a | ||
|