Skip to content

Commit e22ceea

Browse files
committed
Explain surprising new syntax appearing in example code
In a straight-through read of "Syntax and Semantics," the first time we meet a generic, and the first time we meet a vector, is when a Vec<T> shows up in this example. I'm not sure that I could argue that the whole section should appear later in the book than the ones on vectors and generics, so instead just give the reader a brief introduction to both and a promise to follow up later.
1 parent 5daa753 commit e22ceea

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/doc/book/ownership.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,24 @@ fn foo() {
5151
}
5252
```
5353

54-
When `v` comes into scope, a new [`Vec<T>`][vect] is created. In this case, the
55-
vector also allocates space on [the heap][heap], for the three elements. When
56-
`v` goes out of scope at the end of `foo()`, Rust will clean up everything
57-
related to the vector, even the heap-allocated memory. This happens
58-
deterministically, at the end of the scope.
54+
When `v` comes into scope, a new [vector][] is created, and it allocates space
55+
on the heap for each of its elements. When `v` goes out of scope at the end of
56+
`foo()`, Rust will clean up everything related to the vector, even the
57+
heap-allocated memory. This happens deterministically, at the end of the scope.
5958

60-
[vect]: ../std/vec/struct.Vec.html
59+
We'll cover [vectors][vector] in detail later in this chapter; we only use them
60+
here as an example of a type that allocates space on the heap at runtime. They
61+
behave like [arrays][], except their size may change by `push()`ing more
62+
elements onto them.
63+
64+
Vectors have a [generic type][generics] `Vec<T>`, so in this example `v` will have type
65+
`Vec<i32>`. We'll cover generics in detail later in this chapter.
66+
67+
[arrays]: primitive-types.html#arrays
68+
[vector]: vectors.html
6169
[heap]: the-stack-and-the-heap.html
6270
[bindings]: variable-bindings.html
71+
[generics]: generics.html
6372

6473
# Move semantics
6574

0 commit comments

Comments
 (0)