@@ -6,7 +6,7 @@ become quite acquainted. Ownership is how Rust achieves its largest goal,
6
6
memory safety. There are a few distinct concepts, each with its own
7
7
chapter:
8
8
9
- * ownership, which you’re reading now.
9
+ * ownership, which you’re reading now
10
10
* [ borrowing] [ borrowing ] , and their associated feature ‘references’
11
11
* [ lifetimes] [ lifetimes ] , an advanced concept of borrowing
12
12
@@ -23,7 +23,7 @@ Before we get to the details, two important notes about the ownership system.
23
23
Rust has a focus on safety and speed. It accomplishes these goals through many
24
24
‘zero-cost abstractions’, which means that in Rust, abstractions cost as little
25
25
as possible in order to make them work. The ownership system is a prime example
26
- of a zero cost abstraction. All of the analysis we’ll talk about in this guide
26
+ of a zero- cost abstraction. All of the analysis we’ll talk about in this guide
27
27
is _ done at compile time_ . You do not pay any run-time cost for any of these
28
28
features.
29
29
@@ -41,7 +41,7 @@ With that in mind, let’s learn about ownership.
41
41
42
42
# Ownership
43
43
44
- [ ` Variable bindings ` ] [ bindings ] have a property in Rust: they ‘have ownership’
44
+ [ Variable bindings] [ bindings ] have a property in Rust: they ‘have ownership’
45
45
of what they’re bound to. This means that when a binding goes out of scope, the
46
46
resource that they’re bound to are freed. For example:
47
47
@@ -106,8 +106,8 @@ take(v);
106
106
println!("v[0] is: {}", v[0]);
107
107
```
108
108
109
- Same error: “ use of moved value.” When we transfer ownership to something else,
110
- we say that we’ve ‘moved’ the thing we refer to. You don’t need some sort of
109
+ Same error: ‘ use of moved value’. When we transfer ownership to something else,
110
+ we say that we’ve ‘moved’ the thing we refer to. You don’t need any sort of
111
111
special annotation here, it’s the default thing that Rust does.
112
112
113
113
## The details
@@ -121,19 +121,19 @@ let v = vec![1, 2, 3];
121
121
let v2 = v ;
122
122
```
123
123
124
- The first line creates some data for the vector on the [ stack ] [ sh ] , ` v ` . The
125
- vector’s data, however, is stored on the [ heap ] [ sh ] , and so it contains a
126
- pointer to that data . When we move ` v ` to ` v2 ` , it creates a copy of that pointer ,
127
- for ` v2 ` . Which would mean two pointers to the contents of the vector on the
128
- heap. That would be a problem: it would violate Rust’s safety guarantees by
129
- introducing a data race. Therefore, Rust forbids using ` v ` after we’ve done the
130
- move.
124
+ The first line allocates memory for the vector object , ` v ` , and for the data it
125
+ contains. The vector object is stored on the [ stack ] [ sh ] and contains a pointer
126
+ to the content ( ` [1, 2, 3] ` ) stored on the [ heap ] [ sh ] . When we move ` v ` to ` v2 ` ,
127
+ it creates a copy of that pointer, for ` v2 ` . Which means that there would be two
128
+ pointers to the content of the vector on the heap. It would violate Rust’s
129
+ safety guarantees by introducing a data race. Therefore, Rust forbids using ` v `
130
+ after we’ve done the move.
131
131
132
132
[ sh ] : the-stack-and-the-heap.html
133
133
134
134
It’s also important to note that optimizations may remove the actual copy of
135
- the bytes, depending on circumstances. So it may not be as inefficient as it
136
- initially seems.
135
+ the bytes on the stack , depending on circumstances. So it may not be as
136
+ inefficient as it initially seems.
137
137
138
138
## ` Copy ` types
139
139
0 commit comments