You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The inner lists _must_ be an owned pointer, becuase we can't know how many
197
204
elements are in the list. Without knowing the length, we don't know the size,
@@ -207,7 +214,7 @@ proved that it's an issue through benchmarks.
207
214
208
215
For example, this will work:
209
216
210
-
```rust
217
+
~~~rust
211
218
structPoint {
212
219
x:int,
213
220
y:int,
@@ -219,12 +226,12 @@ fn main() {
219
226
println(a.x.to_str());
220
227
}
221
228
}
222
-
```
229
+
~~~
223
230
224
231
This struct is tiny, so it's fine. If `Point` were large, this would be more
225
232
efficient:
226
233
227
-
```rust
234
+
~~~rust
228
235
structPoint {
229
236
x:int,
230
237
y:int,
@@ -236,7 +243,7 @@ fn main() {
236
243
println(a.x.to_str());
237
244
}
238
245
}
239
-
```
246
+
~~~
240
247
241
248
Now it'll be copying a pointer-sized chunk of memory rather than the whole
242
249
struct.
@@ -249,7 +256,7 @@ program is very large and complicated.
249
256
250
257
For example, let's say you're using an owned pointer, and you want to do this:
251
258
252
-
```rust
259
+
~~~rust {.xfail-test}
253
260
structPoint {
254
261
x:int,
255
262
y:int,
@@ -261,22 +268,22 @@ fn main() {
261
268
println(b.x.to_str());
262
269
println(a.x.to_str());
263
270
}
264
-
```
271
+
~~~
265
272
266
273
You'll get this error:
267
274
268
-
```
275
+
~~~{.notrust}
269
276
test.rs:10:12: 10:13 error: use of moved value: `a`
270
277
test.rs:10 println(a.x.to_str());
271
278
^
272
279
test.rs:8:8: 8:9 note: `a` moved here because it has type `~Point`, which is moved by default (use `ref` to override)
273
280
test.rs:8 let b = a;
274
281
^
275
-
```
282
+
~~~
276
283
277
284
As the message says, owned pointers only allow for one owner at a time. When you assign `a` to `b`, `a` becomes invalid. Change your code to this, however:
278
285
279
-
```rust
286
+
~~~rust
280
287
structPoint {
281
288
x:int,
282
289
y:int,
@@ -288,14 +295,14 @@ fn main() {
288
295
println(b.x.to_str());
289
296
println(a.x.to_str());
290
297
}
291
-
```
298
+
~~~
292
299
293
300
And it works:
294
301
295
-
```
302
+
~~~{.notrust}
296
303
10
297
304
10
298
-
```
305
+
~~~
299
306
300
307
So why not just use managed pointers everywhere? There are two big drawbacks to
301
308
managed pointers:
@@ -315,15 +322,15 @@ data they're pointing to. They're just borrowing it for a while. So in that
315
322
sense, they're simple: just keep whatever ownership the data already has. For
0 commit comments