@@ -21,7 +21,7 @@ fn succ(x: &int) -> int { *x + 1 }
21
21
22
22
So I wrote this code to try it out:
23
23
24
- ~~~ rust {.xfail-test}
24
+ ~~~ rust{.xfail-test}
25
25
fn main() {
26
26
let number = 5;
27
27
let succ_number = succ(number);
@@ -73,7 +73,7 @@ However.
73
73
Here are the use-cases for pointers. I've prefixed them with the name of the
74
74
pointer that satisfies that use-case:
75
75
76
- 1 . Owned: ~ Trait must be a pointer, becuase you don't know the size of the
76
+ 1 . Owned: ~ Trait must be a pointer, because you don't know the size of the
77
77
object, so indirection is mandatory.
78
78
2 . Owned: You need a recursive data structure. These can be infinite sized, so
79
79
indirection is mandatory.
@@ -85,18 +85,18 @@ common, such as C++, please read "A note..." below.
85
85
or impossible. This is only often useful when a program is very large or very
86
86
complicated. Using a managed pointer will activate Rust's garbage collection
87
87
mechanism.
88
- 5: Reference: You're writing a function, and you need a pointer, but you don't
88
+ 5 . Reference: You're writing a function, and you need a pointer, but you don't
89
89
care about its ownership. If you make the argument a reference, callers
90
90
can send in whatever kind they want.
91
91
92
- Five exceptions. That's it. Otherwise, you shouldn't need them. Be skeptical
92
+ Five exceptions. That's it. Otherwise, you shouldn't need them. Be sceptical
93
93
of pointers in Rust: use them for a deliberate purpose, not just to make the
94
94
compiler happy.
95
95
96
96
## A note for those proficient in pointers
97
97
98
98
If you're coming to Rust from a language like C or C++, you may be used to
99
- passing things by reference, or passing things by pointer. In some langauges ,
99
+ passing things by reference, or passing things by pointer. In some languages ,
100
100
like Java, you can't even have objects without a pointer to them. Therefore, if
101
101
you were writing this Rust code:
102
102
@@ -150,7 +150,7 @@ fn main() {
150
150
}
151
151
~~~
152
152
153
- But won't this be inefficent ? Well, that's a complicated question, but it's
153
+ But won't this be inefficient ? Well, that's a complicated question, but it's
154
154
important to know that Rust, like C and C++, store aggregate data types
155
155
'unboxed,' whereas languages like Java and Ruby store these types as 'boxed.'
156
156
For smaller structs, this way will be more efficient. For larger ones, it may
@@ -173,7 +173,7 @@ These two properties make for three use cases.
173
173
174
174
## References to Traits
175
175
176
- Traits must be referenced through a pointer, becuase the struct that implements
176
+ Traits must be referenced through a pointer, because the struct that implements
177
177
the trait may be a different size than a different struct that implements the
178
178
trait. Therefore, unboxed traits don't make any sense, and aren't allowed.
179
179
@@ -199,7 +199,7 @@ This prints:
199
199
Cons(1, ~Cons(2, ~Cons(3, ~Nil)))
200
200
~~~
201
201
202
- The inner lists _ must_ be an owned pointer, becuase we can't know how many
202
+ The inner lists _ must_ be an owned pointer, because we can't know how many
203
203
elements are in the list. Without knowing the length, we don't know the size,
204
204
and therefore require the indirection that pointers offer.
205
205
@@ -261,7 +261,7 @@ program is very large and complicated.
261
261
262
262
For example, let's say you're using an owned pointer, and you want to do this:
263
263
264
- ~~~ rust {.xfail-test}
264
+ ~~~ rust{.xfail-test}
265
265
struct Point {
266
266
x: int,
267
267
y: int,
@@ -315,7 +315,7 @@ managed pointers:
315
315
1 . They activate Rust's garbage collector. Other pointer types don't share this
316
316
drawback.
317
317
2 . You cannot pass this data to another task. Shared ownership across
318
- concurrency boundaries is the source of endless pain in other langauges , so
318
+ concurrency boundaries is the source of endless pain in other languages , so
319
319
Rust does not let you do this.
320
320
321
321
# References
@@ -355,7 +355,7 @@ takes in two references, but we give it a managed and unique pointer. Of
355
355
course, if this were a real program, we wouldn't have any of these pointers,
356
356
they're just there to demonstrate the concepts.
357
357
358
- So how is this hard? Well, because we're igorning ownership, the compiler needs
358
+ So how is this hard? Well, because we're ignoring ownership, the compiler needs
359
359
to take great care to make sure that everything is safe. Despite their complete
360
360
safety, a reference's representation at runtime is the same as that of
361
361
an ordinary pointer in a C program. They introduce zero overhead. The compiler
@@ -365,14 +365,14 @@ This theory is called 'region pointers,' and involve a concept called
365
365
'lifetimes'. Here's the simple explanation: would you expect this code to
366
366
compile?
367
367
368
- ~~~ rust {.xfail-test}
368
+ ~~~ rust{.xfail-test}
369
369
fn main() {
370
370
println(x.to_str());
371
371
let x = 5;
372
372
}
373
373
~~~
374
374
375
- Probably not. That's becuase you know that the name ` x ` is valid from where
375
+ Probably not. That's because you know that the name ` x ` is valid from where
376
376
it's declared to when it goes out of scope. In this case, that's the end of
377
377
the ` main ` function. So you know this code will cause an error. We call this
378
378
duration a 'lifetime'. Let's try a more complex example:
@@ -394,7 +394,7 @@ Here, we're borrowing a pointer to `x` inside of the `if`. The compiler, however
394
394
is able to determine that that pointer will go out of scope without ` x ` being
395
395
mutated, and therefore, lets us pass. This wouldn't work:
396
396
397
- ~~~ rust {.xfail-test}
397
+ ~~~ rust{.xfail-test}
398
398
fn main() {
399
399
let mut x = ~5;
400
400
if *x < 10 {
@@ -427,7 +427,7 @@ great detail, so if you want the full details, check that out.
427
427
428
428
# Returning Pointers
429
429
430
- We've talked a lot about funtions that accept various kinds of pointers, but
430
+ We've talked a lot about functions that accept various kinds of pointers, but
431
431
what about returning them? Here's the rule of thumb: only return a unique or
432
432
managed pointer if you were given one in the first place.
433
433
0 commit comments