@@ -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
@@ -407,7 +407,7 @@ ownership of any data it uses; we'll have more on the significance of
407
407
this shortly.) This closure is executed in a new thread created by
408
408
`spawn`.
409
409
410
- One common form of problem in concurrent programs is a ' data race. '
410
+ One common form of problem in concurrent programs is a * data race*.
411
411
This occurs when two different threads attempt to access the same
412
412
location in memory in a non-synchronized way, where at least one of
413
413
them is a write. If one thread is attempting to read, and one thread
@@ -460,9 +460,9 @@ code tries to make three owners. This may cause a safety problem, so
460
460
Rust disallows it.
461
461
462
462
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
463
+ * Arc* stands for "atomically reference counted". In other words, an Arc will
464
464
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
465
+ associated resource until the count is zero. The * atomic* portion refers to an
466
466
Arc' s usage of concurrency primitives to atomically update the count, making it
467
467
safe across threads. If we use an Arc, we can have our three references. But,
468
468
an Arc does not allow mutable borrows of the data it holds, and we want to
@@ -525,13 +525,13 @@ give us assurance _at compile time_ that we weren't doing something incorrect
525
525
with regards to concurrency. In order to share ownership, we were forced to be
526
526
explicit and use a mechanism to ensure that it would be properly handled.
527
527
528
- # Safety _and_ speed
528
+ # Safety _and_ Speed
529
529
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.
530
+ Safety and speed are always presented as a continuum. At one end of the spectrum,
531
+ you have maximum speed, but no safety. On the other end , you have absolute safety
532
+ with no speed. Rust seeks to break out of this paradigm by introducing safety at
533
+ compile time, ensuring that you haven' t done anything wrong, while compiling to
534
+ the same low-level code you' d expect without the safety.
535
535
536
536
As an example, Rust' s ownership system is _entirely_ at compile time. The
537
537
safety check that makes this an error about moved values:
0 commit comments