mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-02 23:38:41 +08:00
Add more filenames; use valid caption markup
This commit is contained in:
parent
220b2c21eb
commit
15397f4f00
@ -75,6 +75,8 @@ discuss in Chapter 10. Lifetimes ensure that the data a struct references is
|
|||||||
valid for as long as the struct is. If you try to store a reference in a struct
|
valid for as long as the struct is. If you try to store a reference in a struct
|
||||||
without specifying lifetimes, like this:
|
without specifying lifetimes, like this:
|
||||||
|
|
||||||
|
Filename: src/main.rs
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
struct User {
|
struct User {
|
||||||
username: &str,
|
username: &str,
|
||||||
@ -410,6 +412,8 @@ Let’s change our `area` function that takes a `Rectangle` instance as an
|
|||||||
argument and instead make an `area` method defined on the `Rectangle` struct,
|
argument and instead make an `area` method defined on the `Rectangle` struct,
|
||||||
as shown in Listing 5-7:
|
as shown in Listing 5-7:
|
||||||
|
|
||||||
|
Filename: src/main.rs
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
@ -511,6 +515,8 @@ another instance of `Rectangle` and return `true` if the second rectangle could
|
|||||||
fit completely within `self` and `false` if it would not. That is, if we run
|
fit completely within `self` and `false` if it would not. That is, if we run
|
||||||
the code in Listing 5-8, once we've defined the `can_hold` method:
|
the code in Listing 5-8, once we've defined the `can_hold` method:
|
||||||
|
|
||||||
|
Filename: src/main.rs
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
fn main() {
|
fn main() {
|
||||||
let rect1 = Rectangle { length: 50, width: 30 };
|
let rect1 = Rectangle { length: 50, width: 30 };
|
||||||
@ -547,7 +553,9 @@ if `self`’s length and width are both greater than the length and width of the
|
|||||||
other `Rectangle`, respectively. Let’s add this new method to the `impl` block
|
other `Rectangle`, respectively. Let’s add this new method to the `impl` block
|
||||||
from Listing 5-7:
|
from Listing 5-7:
|
||||||
|
|
||||||
```
|
Filename: src/main.rs
|
||||||
|
|
||||||
|
```rust
|
||||||
impl Rectangle {
|
impl Rectangle {
|
||||||
fn area(&self) -> u32 {
|
fn area(&self) -> u32 {
|
||||||
self.length * self.width
|
self.length * self.width
|
||||||
@ -579,6 +587,8 @@ that would take one dimension argument and use that as both length and width,
|
|||||||
thus making it easier to create a square `Rectangle` rather than having to
|
thus making it easier to create a square `Rectangle` rather than having to
|
||||||
specify the same value twice:
|
specify the same value twice:
|
||||||
|
|
||||||
|
Filename: src/main.rs
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Rectangle {
|
impl Rectangle {
|
||||||
fn square(size: u32) -> Rectangle {
|
fn square(size: u32) -> Rectangle {
|
||||||
|
Binary file not shown.
@ -24,6 +24,8 @@ of the pieces of data, which we call *fields*, and specify each field’s type.
|
|||||||
For example, Listing 5-1 shows a struct to store information about a user
|
For example, Listing 5-1 shows a struct to store information about a user
|
||||||
account:
|
account:
|
||||||
|
|
||||||
|
<figure>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
struct User {
|
struct User {
|
||||||
username: String,
|
username: String,
|
||||||
@ -33,9 +35,12 @@ struct User {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-1: A `User` struct definition
|
Listing 5-1: A `User` struct definition
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
To use a struct once we've defined it, we create an *instance* of that struct
|
To use a struct once we've defined it, we create an *instance* of that struct
|
||||||
by specifying concrete values for each of the fields. Creating an instance is
|
by specifying concrete values for each of the fields. Creating an instance is
|
||||||
@ -79,6 +84,8 @@ discuss in Chapter 10. Lifetimes ensure that the data a struct references is
|
|||||||
valid for as long as the struct is. If you try to store a reference in a struct
|
valid for as long as the struct is. If you try to store a reference in a struct
|
||||||
without specifying lifetimes, like this:
|
without specifying lifetimes, like this:
|
||||||
|
|
||||||
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
struct User {
|
struct User {
|
||||||
username: &str,
|
username: &str,
|
||||||
@ -128,6 +135,7 @@ the length and width of a rectangle specified in pixels and will calculate the
|
|||||||
area of the rectangle. Listing 5-2 has a short program with one way of doing
|
area of the rectangle. Listing 5-2 has a short program with one way of doing
|
||||||
just that in our project’s *src/main.rs*:
|
just that in our project’s *src/main.rs*:
|
||||||
|
|
||||||
|
<figure>
|
||||||
<span class="filename">Filename: src/main.rs</span>
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -146,10 +154,13 @@ fn area(length: u32, width: u32) -> u32 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-2: Calculating the area of a rectangle specified by its length and
|
Listing 5-2: Calculating the area of a rectangle specified by its length and
|
||||||
width in separate variables
|
width in separate variables
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
Let’s try running this program with `cargo run`:
|
Let’s try running this program with `cargo run`:
|
||||||
|
|
||||||
@ -178,6 +189,7 @@ manageable to group length and width together.
|
|||||||
We’ve already discussed one way we might do that in Chapter 3: tuples. Listing
|
We’ve already discussed one way we might do that in Chapter 3: tuples. Listing
|
||||||
5-3 has a version of our program which uses tuples:
|
5-3 has a version of our program which uses tuples:
|
||||||
|
|
||||||
|
<figure>
|
||||||
<span class="filename">Filename: src/main.rs</span>
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -195,9 +207,12 @@ fn area(dimensions: (u32, u32)) -> u32 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-3: Specifying the length and width of the rectangle with a tuple
|
Listing 5-3: Specifying the length and width of the rectangle with a tuple
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
<!-- I will add ghosting & wingdings once we're in libreoffice /Carol -->
|
<!-- I will add ghosting & wingdings once we're in libreoffice /Carol -->
|
||||||
|
|
||||||
@ -227,6 +242,7 @@ Here is where we bring in structs. We can transform our tuple into a data type
|
|||||||
with a name for the whole as well as names for the parts, as shown in Listing
|
with a name for the whole as well as names for the parts, as shown in Listing
|
||||||
5-4:
|
5-4:
|
||||||
|
|
||||||
|
<figure>
|
||||||
<span class="filename">Filename: src/main.rs</span>
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -249,9 +265,12 @@ fn area(rectangle: &Rectangle) -> u32 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-4: Defining a `Rectangle` struct
|
Listing 5-4: Defining a `Rectangle` struct
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
<!-- Will add ghosting & wingdings once we're in libreoffice /Carol -->
|
<!-- Will add ghosting & wingdings once we're in libreoffice /Carol -->
|
||||||
|
|
||||||
@ -279,6 +298,7 @@ It’d be nice to be able to print out an instance of our `Rectangle` while we
|
|||||||
debugging our program and see the values for all its fields. Listing 5-5 tries
|
debugging our program and see the values for all its fields. Listing 5-5 tries
|
||||||
using the `println!` macro as we have been:
|
using the `println!` macro as we have been:
|
||||||
|
|
||||||
|
<figure>
|
||||||
<span class="filename">Filename: src/main.rs</span>
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
@ -294,9 +314,12 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-5: Attempting to print a `Rectangle` instance
|
Listing 5-5: Attempting to print a `Rectangle` instance
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
If we run this, we get an error with this core message:
|
If we run this, we get an error with this core message:
|
||||||
|
|
||||||
@ -346,6 +369,8 @@ have to explicitly opt-in to having that functionality be available for our
|
|||||||
struct. To do that, we add the annotation `#[derive(Debug)]` just before our
|
struct. To do that, we add the annotation `#[derive(Debug)]` just before our
|
||||||
struct definition, as shown in Listing 5-6. Now our program looks like this:
|
struct definition, as shown in Listing 5-6. Now our program looks like this:
|
||||||
|
|
||||||
|
<figure>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
@ -360,10 +385,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-6: Adding the annotation to derive the `Debug` trait and printing the
|
Listing 5-6: Adding the annotation to derive the `Debug` trait and printing the
|
||||||
`Rectangle` instance using debug formatting
|
`Rectangle` instance using debug formatting
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
At this point, if we run this program, we won’t get any errors and we’ll see
|
At this point, if we run this program, we won’t get any errors and we’ll see
|
||||||
the following output:
|
the following output:
|
||||||
|
@ -14,6 +14,9 @@ Let’s change our `area` function that takes a `Rectangle` instance as an
|
|||||||
argument and instead make an `area` method defined on the `Rectangle` struct,
|
argument and instead make an `area` method defined on the `Rectangle` struct,
|
||||||
as shown in Listing 5-7:
|
as shown in Listing 5-7:
|
||||||
|
|
||||||
|
<figure>
|
||||||
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
@ -37,9 +40,12 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-7: Defining an `area` method on the `Rectangle` struct
|
Listing 5-7: Defining an `area` method on the `Rectangle` struct
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
<!-- Will add ghosting and wingdings here in libreoffice /Carol -->
|
<!-- Will add ghosting and wingdings here in libreoffice /Carol -->
|
||||||
|
|
||||||
@ -131,6 +137,9 @@ another instance of `Rectangle` and return `true` if the second rectangle could
|
|||||||
fit completely within `self` and `false` if it would not. That is, if we run
|
fit completely within `self` and `false` if it would not. That is, if we run
|
||||||
the code in Listing 5-8, once we've defined the `can_hold` method:
|
the code in Listing 5-8, once we've defined the `can_hold` method:
|
||||||
|
|
||||||
|
<figure>
|
||||||
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
fn main() {
|
fn main() {
|
||||||
let rect1 = Rectangle { length: 50, width: 30 };
|
let rect1 = Rectangle { length: 50, width: 30 };
|
||||||
@ -142,9 +151,12 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<caption>
|
<figcaption>
|
||||||
|
|
||||||
Listing 5-8: Demonstration of using the as-yet-unwritten `can_hold` method
|
Listing 5-8: Demonstration of using the as-yet-unwritten `can_hold` method
|
||||||
</caption>
|
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
|
||||||
We want to see this output, since both of `rect2`’s dimensions are smaller than
|
We want to see this output, since both of `rect2`’s dimensions are smaller than
|
||||||
`rect1`’s, but `rect3` is wider than `rect1`:
|
`rect1`’s, but `rect3` is wider than `rect1`:
|
||||||
@ -167,6 +179,8 @@ if `self`’s length and width are both greater than the length and width of the
|
|||||||
other `Rectangle`, respectively. Let’s add this new method to the `impl` block
|
other `Rectangle`, respectively. Let’s add this new method to the `impl` block
|
||||||
from Listing 5-7:
|
from Listing 5-7:
|
||||||
|
|
||||||
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# #[derive(Debug)]
|
# #[derive(Debug)]
|
||||||
# struct Rectangle {
|
# struct Rectangle {
|
||||||
@ -205,6 +219,8 @@ that would take one dimension argument and use that as both length and width,
|
|||||||
thus making it easier to create a square `Rectangle` rather than having to
|
thus making it easier to create a square `Rectangle` rather than having to
|
||||||
specify the same value twice:
|
specify the same value twice:
|
||||||
|
|
||||||
|
<span class="filename">Filename: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# #[derive(Debug)]
|
# #[derive(Debug)]
|
||||||
# struct Rectangle {
|
# struct Rectangle {
|
||||||
|
Loading…
Reference in New Issue
Block a user