mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-02 23:38:41 +08:00
Add caption tags since I started doing that but never fixed these
This commit is contained in:
parent
032b2cf8be
commit
54ab112f40
@ -274,8 +274,10 @@ is the memory that holds the contents, and this is on the heap.
|
||||
|
||||
<img alt="String in memory" src="img/trpl04-01.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-1: Representation in memory of a `String` holding the value `"hello"`
|
||||
bound to `s1`
|
||||
</caption>
|
||||
|
||||
The length is how much memory, in bytes, the contents of the `String` is
|
||||
currently using. The capacity is the total amount of memory, in bytes, that the
|
||||
@ -290,8 +292,10 @@ words, it looks like figure 4-2.
|
||||
|
||||
<img alt="s1 and s2 pointing to the same value" src="img/trpl04-02.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-2: Representation in memory of the variable `s2` that has a copy of
|
||||
`s1`’s pointer, length and capacity
|
||||
</caption>
|
||||
|
||||
And *not* Figure 4-3, which is what memory would look like if Rust instead
|
||||
copied the heap data as well. If Rust did this, the operation `s2 = s1` could
|
||||
@ -299,8 +303,10 @@ potentially be very expensive if the data on the heap was large.
|
||||
|
||||
<img alt="s1 and s2 to two places" src="img/trpl04-03.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-3: Another possibility for what `s2 = s1` might do, if Rust chose to
|
||||
copy heap data as well.
|
||||
</caption>
|
||||
|
||||
Earlier, we said that when a variable goes out of scope, Rust will
|
||||
automatically call the `drop` function and clean up the heap memory for that
|
||||
@ -344,7 +350,9 @@ into `s2`. So what actually happens looks like Figure 4-4.
|
||||
|
||||
<img alt="s1 moved to s2" src="img/trpl04-04.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-4: Representation in memory after `s1` has been invalidated
|
||||
</caption>
|
||||
|
||||
That solves our problem! With only `s2` valid, when it goes out of scope, it
|
||||
alone will free the memory, and we’re done.
|
||||
@ -565,7 +573,9 @@ taking ownership of it. Figure 4-5 shows a diagram of this.
|
||||
|
||||
<img alt="&String s pointing at String s1" src="img/trpl04-05.svg" class="center" />
|
||||
|
||||
<caption>
|
||||
Figure 4-5: `&String s` pointing at `String s1`
|
||||
</caption>
|
||||
|
||||
Let’s take a closer look at the function call here:
|
||||
|
||||
@ -970,7 +980,9 @@ Figure 4-6 shows this in a diagram:
|
||||
|
||||
<img alt="world containing a pointer to the 6th byte of String s and a length 5" src="img/trpl04-06.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-6: String slice referring to part of a `String`
|
||||
</caption>
|
||||
|
||||
With Rust’s `..` range syntax, if you want to start at the first index (zero),
|
||||
you can drop the value before the `..`. In other words, these are equal:
|
||||
|
@ -262,8 +262,10 @@ is the memory that holds the contents, and this is on the heap.
|
||||
|
||||
<img alt="String in memory" src="img/trpl04-01.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-1: Representation in memory of a `String` holding the value `"hello"`
|
||||
bound to `s1`
|
||||
</caption>
|
||||
|
||||
The length is how much memory, in bytes, the contents of the `String` is
|
||||
currently using. The capacity is the total amount of memory, in bytes, that the
|
||||
@ -278,8 +280,10 @@ words, it looks like figure 4-2.
|
||||
|
||||
<img alt="s1 and s2 pointing to the same value" src="img/trpl04-02.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-2: Representation in memory of the variable `s2` that has a copy of
|
||||
`s1`’s pointer, length and capacity
|
||||
</caption>
|
||||
|
||||
And *not* Figure 4-3, which is what memory would look like if Rust instead
|
||||
copied the heap data as well. If Rust did this, the operation `s2 = s1` could
|
||||
@ -287,8 +291,10 @@ potentially be very expensive if the data on the heap was large.
|
||||
|
||||
<img alt="s1 and s2 to two places" src="img/trpl04-03.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-3: Another possibility for what `s2 = s1` might do, if Rust chose to
|
||||
copy heap data as well.
|
||||
</caption>
|
||||
|
||||
Earlier, we said that when a variable goes out of scope, Rust will automatically
|
||||
call the `drop` function and clean up the heap memory for that variable. But
|
||||
@ -332,7 +338,9 @@ into `s2`. So what actually happens looks like Figure 4-4.
|
||||
|
||||
<img alt="s1 moved to s2" src="img/trpl04-04.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-4: Representation in memory after `s1` has been invalidated
|
||||
</caption>
|
||||
|
||||
That solves our problem! With only `s2` valid, when it goes out of scope, it
|
||||
alone will free the memory, and we’re done.
|
||||
|
@ -35,7 +35,9 @@ without taking ownership of it. Figure 4-5 shows a diagram of this.
|
||||
|
||||
<img alt="&String s pointing at String s1" src="img/trpl04-05.svg" class="center" />
|
||||
|
||||
<caption>
|
||||
Figure 4-5: `&String s` pointing at `String s1`
|
||||
</caption>
|
||||
|
||||
Let’s take a closer look at the function call here:
|
||||
|
||||
|
@ -152,7 +152,9 @@ Figure 4-6 shows this in a diagram:
|
||||
|
||||
<img alt="world containing a pointer to the 6th byte of String s and a length 5" src="img/trpl04-06.svg" class="center" style="width: 50%;" />
|
||||
|
||||
<caption>
|
||||
Figure 4-6: String slice referring to part of a `String`
|
||||
</caption>
|
||||
|
||||
With Rust’s `..` range syntax, if you want to start at the first index (zero),
|
||||
you can drop the value before the `..`. In other words, these are equal:
|
||||
|
Loading…
Reference in New Issue
Block a user