@@ -5,7 +5,7 @@ accomplishes these goals by being memory safe without using garbage collection.
5
5
6
6
This introduction will give you a rough idea of what Rust is like, eliding many
7
7
details. It does not require prior experience with systems programming, but you
8
- may find the syntax easier if you've used a ' curly brace' programming language
8
+ may find the syntax easier if you've used a " curly brace" programming language
9
9
before, like C or JavaScript. The concepts are more important than the syntax,
10
10
so don't worry if you don't get every last detail: you can read [ The
11
11
Rust Programming Language] ( book/index.html ) to get a more complete explanation.
@@ -15,7 +15,7 @@ Rust to follow along. If you'd like to anyway, check out [the
15
15
homepage] ( http://rust-lang.org ) for explanation.
16
16
17
17
To show off Rust, let's talk about how easy it is to get started with Rust.
18
- Then, we'll talk about Rust's most interesting feature, ** ownership* * , and
18
+ Then, we'll talk about Rust's most interesting feature, * ownership* , and
19
19
then discuss how it makes concurrency easier to reason about. Finally,
20
20
we'll talk about how Rust breaks down the perceived dichotomy between speed
21
21
and safety.
@@ -57,7 +57,7 @@ version = "0.0.1"
57
57
authors = ["Your Name <[email protected] >"]
58
58
```
59
59
60
- This is called a ** manifest* * , and it contains all of the metadata that Cargo
60
+ This is called a * manifest* , and it contains all of the metadata that Cargo
61
61
needs to compile your project.
62
62
63
63
Here's what's in ` src/main.rs ` :
@@ -68,7 +68,7 @@ fn main() {
68
68
}
69
69
```
70
70
71
- Cargo generated a 'hello world' for us. We'll talk more about the syntax here
71
+ Cargo generated a "Hello World" for us. We'll talk more about the syntax here
72
72
later, but that's what Rust code looks like! Let's compile and run it:
73
73
74
74
``` {bash}
@@ -146,8 +146,8 @@ Enough about tools, let's talk code!
146
146
147
147
# Ownership
148
148
149
- Rust's defining feature is ' memory safety without garbage collection.' Let's
150
- take a moment to talk about what that means. ** Memory safety* * means that the
149
+ Rust's defining feature is " memory safety without garbage collection". Let's
150
+ take a moment to talk about what that means. * Memory safety* means that the
151
151
programming language eliminates certain kinds of bugs, such as [ buffer
152
152
overflows] ( http://en.wikipedia.org/wiki/Buffer_overflow ) and [ dangling
153
153
pointers] ( http://en.wikipedia.org/wiki/Dangling_pointer ) . These problems occur
@@ -170,7 +170,7 @@ We make an array, `v`, and then call `push` on it. `push` is a method which
170
170
adds an element to the end of an array.
171
171
172
172
Next, we make a new variable, ` x ` , that's equal to the first element of
173
- the array. Simple, but this is where the ' bug' will appear.
173
+ the array. Simple, but this is where the " bug" will appear.
174
174
175
175
Let's keep going. We then call ` push ` again, pushing "world" onto the
176
176
end of the array. ` v ` now is ` ["Hello", "world"] ` .
@@ -222,7 +222,7 @@ its length changes, we may need to allocate more memory. In Ruby, this happens
222
222
as well, we just don't think about it very often. So why does the C++ version
223
223
segfault when we allocate more memory?
224
224
225
- The answer is that in the C++ version, ` x ` is a ** reference* * to the memory
225
+ The answer is that in the C++ version, ` x ` is a * reference* to the memory
226
226
location where the first element of the array is stored. But in Ruby, ` x ` is a
227
227
standalone value, not connected to the underyling array at all. Let's dig into
228
228
the details for a moment. Your program has access to memory, provided to it by
@@ -332,11 +332,11 @@ error: aborting due to previous error
332
332
333
333
When we try to mutate the array by ` push` ing it the second time, Rust throws
334
334
an error. It says that we " cannot borrow v as mutable because it is also
335
- borrowed as immutable." What' s up with "borrowed"?
335
+ borrowed as immutable." What does it mean by " borrowed" ?
336
336
337
- In Rust, the type system encodes the notion of ** ownership* *. The variable `v`
338
- is an " owner" of the vector. When we make a reference to `v`, we let that
339
- variable (in this case, `x`) ' borrow' it for a while. Just like if you own a
337
+ In Rust, the type system encodes the notion of * ownership* . The variable ` v`
338
+ is an * owner* of the vector. When we make a reference to ` v` , we let that
339
+ variable (in this case, ` x` ) * borrow* it for a while. Just like if you own a
340
340
book, and you lend it to me, I' m borrowing the book.
341
341
342
342
So, when I try to modify the vector with the second call to `push`, I need
@@ -392,22 +392,23 @@ Here's an example of a concurrent Rust program:
392
392
use std::thread::Thread;
393
393
394
394
fn main () {
395
- for _ in range(0u, 10u) {
396
- Thread::spawn(move || {
395
+ let guards: Vec < _ > = (0..10).map( | _ | {
396
+ Thread::scoped( || {
397
397
println! (" Hello, world!" );
398
- });
399
- }
398
+ })
399
+ }).collect() ;
400
400
}
401
401
` ` `
402
402
403
- This program creates ten threads, who all print `Hello, world!`. The
404
- `spawn` function takes one argument, a closure, indicated by the
405
- double bars `||`. (The `move` keyword indicates that the closure takes
406
- ownership of any data it uses; we' ll have more on the significance of
407
- this shortly.) This closure is executed in a new thread created by
408
- ` spawn` .
403
+ This program creates ten threads, which all print ` Hello, world! ` . The ` scoped`
404
+ function takes one argument, a closure, indicated by the double bars ` || ` . This
405
+ closure is executed in a new thread created by ` scoped` . The method is called
406
+ ` scoped` because it returns a ' join guard' , which will automatically join the
407
+ child thread when it goes out of scope. Because we ` collect` these guards into
408
+ a ` Vec< T> ` , and that vector goes out of scope at the end of our program, our
409
+ program will wait for every thread to finish before finishing.
409
410
410
- One common form of problem in concurrent programs is a ' data race. '
411
+ One common form of problem in concurrent programs is a * data race* .
411
412
This occurs when two different threads attempt to access the same
412
413
location in memory in a non-synchronized way, where at least one of
413
414
them is a write. If one thread is attempting to read, and one thread
@@ -460,9 +461,9 @@ code tries to make three owners. This may cause a safety problem, so
460
461
Rust disallows it.
461
462
462
463
What to do here? Rust has two types that helps us: ` Arc< T> ` and ` Mutex< T> ` .
463
- " Arc" stands for " atomically reference counted. " In other words, an Arc will
464
+ * Arc* stands for " atomically reference counted" . In other words, an Arc will
464
465
keep track of the number of references to something, and not free the
465
- associated resource until the count is zero. The ' atomic' portion refers to an
466
+ associated resource until the count is zero. The * atomic* portion refers to an
466
467
Arc' s usage of concurrency primitives to atomically update the count, making it
467
468
safe across threads. If we use an Arc, we can have our three references. But,
468
469
an Arc does not allow mutable borrows of the data it holds, and we want to
@@ -525,13 +526,13 @@ give us assurance _at compile time_ that we weren't doing something incorrect
525
526
with regards to concurrency. In order to share ownership, we were forced to be
526
527
explicit and use a mechanism to ensure that it would be properly handled.
527
528
528
- # Safety _and_ speed
529
+ # Safety _and_ Speed
529
530
530
- Safety and speed are always presented as a continuum. On one hand, you have
531
- maximum speed, but no safety. On the other, you have absolute safety, with no
532
- speed. Rust seeks to break out of this mode by introducing safety at compile
533
- time, ensuring that you haven' t done anything wrong, while compiling to the
534
- same low-level code you' d expect without the safety.
531
+ Safety and speed are always presented as a continuum. At one end of the spectrum,
532
+ you have maximum speed, but no safety. On the other end , you have absolute safety
533
+ with no speed. Rust seeks to break out of this paradigm by introducing safety at
534
+ compile time, ensuring that you haven' t done anything wrong, while compiling to
535
+ the same low-level code you' d expect without the safety.
535
536
536
537
As an example, Rust' s ownership system is _entirely_ at compile time. The
537
538
safety check that makes this an error about moved values:
0 commit comments