mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-23 23:50:25 +08:00
Fix merging of figures
This commit is contained in:
parent
d26cdc7508
commit
e4ddcabf6f
@ -261,6 +261,7 @@ length, and a capacity. This group of data is stored on the stack. On the right
|
||||
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%;" />
|
||||
|
||||
Figure 4-1: Representation in memory of a `String` holding the value "hello"
|
||||
bound to `s1`
|
||||
|
||||
@ -276,6 +277,7 @@ copy the data on the heap that the `String`'s pointer refers to. In other
|
||||
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%;" />
|
||||
|
||||
Figure 4-2: Representation in memory of the binding `s2` that has a copy of
|
||||
`s1`'s pointer, length and capacity
|
||||
|
||||
@ -329,6 +331,7 @@ it's known as a _move_. Here we would read this by saying that `s1` was _moved_
|
||||
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%;" />
|
||||
|
||||
Figure 4-4: Representation in memory after `s1` has been invalidated
|
||||
|
||||
That solves our problem! With only `s2` valid, when it goes out of scope, it
|
||||
|
@ -35,6 +35,7 @@ These `&`s are *references*, and they allow you to refer to some value
|
||||
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" />
|
||||
|
||||
Figure 4-5: `&String s` pointing at `String s1`
|
||||
|
||||
Let’s take a closer look at the function call here:
|
||||
|
@ -148,10 +148,11 @@ slice data structure actually stores the starting position and the length of the
|
||||
slice. So in the case of `let world = &s[6..11];`, `world` would be a slice that
|
||||
contains a pointer to the 6th byte of `s` and a length value of 5.
|
||||
|
||||
Figure 4-7 shows this in a diagram:
|
||||
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%;" />
|
||||
Figure 4-7: Two string slices referring to parts of a `String`
|
||||
|
||||
Figure 4-6: Two string slices referring to parts of a `String`
|
||||
|
||||
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