-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb55d3d
commit 8a0bb3c
Showing
11 changed files
with
160 additions
and
155 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 |
---|---|---|
|
@@ -5,13 +5,13 @@ struct User { | |
sign_in_count: u64, | ||
} | ||
|
||
// ANCHOR: here | ||
fn main() { | ||
// ANCHOR: here | ||
let user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
// ANCHOR_END: here | ||
} | ||
// ANCHOR_END: here |
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 |
---|---|---|
|
@@ -5,8 +5,8 @@ struct User { | |
sign_in_count: u64, | ||
} | ||
|
||
// ANCHOR: here | ||
fn main() { | ||
// ANCHOR: here | ||
let mut user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
|
@@ -15,5 +15,5 @@ fn main() { | |
}; | ||
|
||
user1.email = String::from("[email protected]"); | ||
// ANCHOR_END: here | ||
} | ||
// ANCHOR_END: here |
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 |
---|---|---|
|
@@ -5,20 +5,24 @@ struct User { | |
sign_in_count: u64, | ||
} | ||
|
||
// ANCHOR: here | ||
fn main() { | ||
// --snip-- | ||
// ANCHOR_END: here | ||
|
||
let user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
|
||
// ANCHOR: here | ||
|
||
let user2 = User { | ||
active: user1.active, | ||
username: user1.username, | ||
email: String::from("[email protected]"), | ||
sign_in_count: user1.sign_in_count, | ||
}; | ||
// ANCHOR_END: here | ||
} | ||
// ANCHOR_END: here |
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 |
---|---|---|
|
@@ -5,18 +5,22 @@ struct User { | |
sign_in_count: u64, | ||
} | ||
|
||
// ANCHOR: here | ||
fn main() { | ||
// --snip-- | ||
// ANCHOR_END: here | ||
|
||
let user1 = User { | ||
email: String::from("[email protected]"), | ||
username: String::from("someusername123"), | ||
active: true, | ||
sign_in_count: 1, | ||
}; | ||
|
||
// ANCHOR: here | ||
|
||
let user2 = User { | ||
email: String::from("[email protected]"), | ||
..user1 | ||
}; | ||
// ANCHOR_END: here | ||
} | ||
// ANCHOR_END: here |
8 changes: 3 additions & 5 deletions
8
...ings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/src/main.rs
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 |
---|---|---|
@@ -1,9 +1,7 @@ | ||
fn main() { | ||
// ANCHOR: here | ||
struct Color(i32, i32, i32); | ||
struct Point(i32, i32, i32); | ||
struct Color(i32, i32, i32); | ||
struct Point(i32, i32, i32); | ||
|
||
fn main() { | ||
let black = Color(0, 0, 0); | ||
let origin = Point(0, 0, 0); | ||
// ANCHOR_END: here | ||
} |
6 changes: 2 additions & 4 deletions
6
.../ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/src/main.rs
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
fn main() { | ||
// ANCHOR: here | ||
struct AlwaysEqual; | ||
struct AlwaysEqual; | ||
|
||
fn main() { | ||
let subject = AlwaysEqual; | ||
// ANCHOR_END: here | ||
} |
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
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
# Using Structs to Structure Related Data | ||
|
||
A *struct*, or *structure*, is a custom data type that lets you name and | ||
package together multiple related values that make up a meaningful group. If | ||
A *struct*, or *structure*, is a custom data type that lets you package | ||
together and name multiple related values that make up a meaningful group. If | ||
you’re familiar with an object-oriented language, a *struct* is like an | ||
object’s data attributes. In this chapter, we’ll compare and contrast tuples | ||
with structs. We’ll demonstrate how to define and instantiate structs. We’ll | ||
discuss how to define associated functions, especially the kind of associated | ||
functions called *methods*, to specify behavior associated with a struct type. | ||
Structs and enums (discussed in Chapter 6) are the building blocks for creating | ||
new types in your program’s domain to take full advantage of Rust’s compile | ||
time type checking. | ||
with structs to build on what you already know and demonstrate when structs are | ||
a better way to group data. We’ll demonstrate how to define and instantiate | ||
structs. We’ll discuss how to define associated functions, especially the kind | ||
of associated functions called *methods*, to specify behavior associated with a | ||
struct type. Structs and enums (discussed in Chapter 6) are the building blocks | ||
for creating new types in your program’s domain to take full advantage of | ||
Rust’s compile time type checking. |
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
Oops, something went wrong.