Skip to content

Commit 6790b0e

Browse files
committed
Auto merge of rust-lang#24328 - Manishearth:rollup, r=Manishearth
2 parents 67a8f61 + 0a2885a commit 6790b0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1592
-1529
lines changed

src/doc/trpl/README.md

+178-25
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,192 @@
11
% The Rust Programming Language
22

3-
Welcome! This book will teach you about [the Rust Programming
4-
Language](http://www.rust-lang.org/). Rust is a modern systems programming
5-
language focusing on safety and speed. It accomplishes these goals by being
6-
memory safe without using garbage collection.
3+
Welcome! This book will teach you about the [Rust Programming Language][rust].
4+
Rust is a systems programming language focused on three goals: safety, speed,
5+
and concurrency. It maintains these goals without having a garbage collector,
6+
making it a useful language for a number of use cases other languages aren’t
7+
good at: embedding in other languages, programs with specific space and time
8+
requirements, and writing low-level code, like device drivers and operating
9+
systems. It improves on current languages targeting this space by having a
10+
number of compile-time safety checks that produce no runtime overhead, while
11+
eliminating all data races. Rust also aims to achieve ‘zero-cost abstrations’
12+
even though some of these abstractions feel like those of a high-level
13+
language. Even then, Rust still allows precise control like a low-level
14+
language would.
715

8-
"The Rust Programming Language" is split into three sections, which you can
9-
navigate through the menu on the left.
16+
[rust]: http://rust-lang.org
1017

11-
<h2 class="section-header"><a href="basic.html">Basics</a></h2>
18+
“The Rust Programming Language” is split into seven sections. This introduction
19+
is the first. After this:
1220

13-
This section is a linear introduction to the basic syntax and semantics of
14-
Rust. It has individual sections on each part of Rust's syntax.
21+
* [Getting started][gs] - Set up your computer for Rust development.
22+
* [Learn Rust][lr] - Learn Rust programming through small projects.
23+
* [Effective Rust][er] - Higher-level concepts for writing excellent Rust code.
24+
* [Syntax and Semantics][ss] - Each bit of Rust, broken down into small chunks.
25+
* [Nightly Rust][nr] - Cutting-edge features that aren’t in stable builds yet.
26+
* [Glossary][gl] - A reference of terms used in the book.
1527

16-
After reading "Basics," you will have a good foundation to learn more about
17-
Rust, and can write very simple programs.
28+
[gs]: getting-started.html
29+
[lr]: learn-rust.html
30+
[er]: effective-rust.html
31+
[ss]: syntax-and-semantics.html
32+
[nr]: nightly-rust.html
33+
[gl]: glossary.html
1834

19-
<h2 class="section-header"><a href="intermediate.html">Intermediate</a></h2>
35+
After reading this introduction, you’ll want to dive into either ‘Learn Rust’
36+
or ‘Syntax and Semantics’, depending on your preference: ‘Learn Rust’ if you
37+
want to dive in with a project, or ‘Syntax and Semantics’ if you prefer to
38+
start small, and learn a single concept thoroughly before moving onto the next.
39+
Copious cross-linking connects these parts together.
2040

21-
This section contains individual chapters, which are self-contained. They focus
22-
on specific topics, and can be read in any order.
41+
## A brief introduction to Rust
2342

24-
After reading "Intermediate," you will have a solid understanding of Rust,
25-
and will be able to understand most Rust code and write more complex programs.
43+
Is Rust a language you might be interested in? Let’s examine a few small code
44+
samples to show off a few of its strengths.
2645

27-
<h2 class="section-header"><a href="advanced.html">Advanced</a></h2>
46+
The main concept that makes Rust unique is called ‘ownership’. Consider this
47+
small example:
2848

29-
In a similar fashion to "Intermediate," this section is full of individual,
30-
deep-dive chapters, which stand alone and can be read in any order. These
31-
chapters focus on Rust's most complex features.
49+
```rust
50+
fn main() {
51+
let mut x = vec!["Hello", "world"];
52+
}
53+
```
3254

33-
<h2 class="section-header"><a href="unstable.html">Unstable</a></h2>
55+
This program makes a [variable binding][var] named `x`. The value of this
56+
binding is a `Vec<T>`, a ‘vector’, that we create through a [macro][macro]
57+
defined in the standard library. This macro is called `vec`, and we invoke
58+
macros with a `!`. This follows a general principle of Rust: make things
59+
explicit. Macros can do significantly more complicated things than function
60+
calls, and so they’re visually distinct. The `!` also helps with parsing,
61+
making tooling easier to write, which is also important.
3462

35-
In a similar fashion to "Intermediate," this section is full of individual,
36-
deep-dive chapters, which stand alone and can be read in any order.
63+
We used `mut` to make `x` mutable: bindings are immutable by default in Rust.
64+
We’ll be mutating this vector later in the example.
3765

38-
This chapter contains things that are only available on the nightly channel of
39-
Rust.
66+
It’s also worth noting that we didn’t need a type annotation here: while Rust
67+
is statically typed, we didn’t need to explicitly annotate the type. Rust has
68+
type inference to balance out the power of static typing with the verbosity of
69+
annotating types.
70+
71+
Rust prefers stack allocation to heap allocation: `x` is placed directly on the
72+
stack. However, the `Vec<T>` type allocates space for the elements of the
73+
vector on the heap. If you’re not familiar with this distinction, you can
74+
ignore it for now, or check out [‘The Stack and the Heap’][heap]. As a systems
75+
programming language, Rust gives you the ability to control how your memory is
76+
allocated, but when we’re getting started, it’s less of a big deal.
77+
78+
[var]: variable-bindings.html
79+
[macro]: macros.html
80+
[heap]: the-stack-and-the-heap.html
81+
82+
Earlier, we mentioned that ‘ownership’ is the key new concept in Rust. In Rust
83+
parlance, `x` is said to ‘own’ the vector. This means that when `x` goes out of
84+
scope, the vector’s memory will be de-allocated. This is done deterministically
85+
by the Rust compiler, rather than through a mechanism such as a garbage
86+
collector. In other words, in Rust, you don’t call functions like `malloc` and
87+
`free` yourself: the compiler statically determines when you need to allocate
88+
or deallocate memory, and inserts those calls itself. To err is to be human,
89+
but compilers never forget.
90+
91+
Let’s add another line to our example:
92+
93+
```rust
94+
fn main() {
95+
let mut x = vec!["Hello", "world"];
96+
97+
let y = &x[0];
98+
}
99+
```
100+
101+
We’ve introduced another binding, `y`. In this case, `y` is a ‘reference’ to
102+
the first element of the vector. Rust’s references are similar to pointers in
103+
other languages, but with additional compile-time safety checks. References
104+
interact with the ownership system by [‘borrowing’][borrowing] what they point
105+
to, rather than owning it. The difference is, when the reference goes out of
106+
scope, it will not deallocate the underlying memory. If it did, we’d
107+
de-allocate twice, which is bad!
108+
109+
[borrowing]: references-and-borrowing.html
110+
111+
Let’s add a third line. It looks innocent enough, but causes a compiler error:
112+
113+
```rust,ignore
114+
fn main() {
115+
let mut x = vec!["Hello", "world"];
116+
117+
let y = &x[0];
118+
119+
x.push("foo");
120+
}
121+
```
122+
123+
`push` is a method on vectors that appends another element to the end of the
124+
vector. When we try to compile this program, we get an error:
125+
126+
```text
127+
error: cannot borrow `x` as mutable because it is also borrowed as immutable
128+
x.push(4);
129+
^
130+
note: previous borrow of `x` occurs here; the immutable borrow prevents
131+
subsequent moves or mutable borrows of `x` until the borrow ends
132+
let y = &x[0];
133+
^
134+
note: previous borrow ends here
135+
fn main() {
136+
137+
}
138+
^
139+
```
140+
141+
Whew! The Rust compiler gives quite detailed errors at times, and this is one
142+
of those times. As the error explains, while we made our binding mutable, we
143+
still cannot call `push`. This is because we already have a reference to an
144+
element of the vector, `y`. Mutating something while another reference exists
145+
is dangerous, because we may invalidate the reference. In this specific case,
146+
when we create the vector, we may have only allocated space for three elements.
147+
Adding a fourth would mean allocating a new chunk of memory for all those elements,
148+
copying the old values over, and updating the internal pointer to that memory.
149+
That all works just fine. The problem is that `y` wouldn’t get updated, and so
150+
we’d have a ‘dangling pointer’. That’s bad. Any use of `y` would be an error in
151+
this case, and so the compiler has caught this for us.
152+
153+
So how do we solve this problem? There are two approaches we can take. The first
154+
is making a copy rather than using a reference:
155+
156+
```rust
157+
fn main() {
158+
let mut x = vec!["Hello", "world"];
159+
160+
let y = x[0].clone();
161+
162+
x.push("foo");
163+
}
164+
```
165+
166+
Rust has [move semantics][move] by default, so if we want to make a copy of some
167+
data, we call the `clone()` method. In this example, `y` is no longer a reference
168+
to the vector stored in `x`, but a copy of its first element, `"hello"`. Now
169+
that we don’t have a reference, our `push()` works just fine.
170+
171+
[move]: move-semantics.html
172+
173+
If we truly want a reference, we need the other option: ensure that our reference
174+
goes out of scope before we try to do the mutation. That looks like this:
175+
176+
```rust
177+
fn main() {
178+
let mut x = vec!["Hello", "world"];
179+
180+
{
181+
let y = &x[0];
182+
}
183+
184+
x.push("foo");
185+
}
186+
```
187+
188+
We created an inner scope with an additional set of curly braces. `y` will go out of
189+
scope before we call `push()`, and so we’re all good.
190+
191+
This concept of ownership isn’t just good for preventing danging pointers, but an
192+
entire set of related problems, like iterator invalidation, concurrency, and more.

src/doc/trpl/SUMMARY.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,45 @@
77
* [Learn Rust](learn-rust.md)
88
* [Effective Rust](effective-rust.md)
99
* [The Stack and the Heap](the-stack-and-the-heap.md)
10-
* [`Debug` and `Display`](debug-and-display.md)
10+
* [Debug and Display](debug-and-display.md)
1111
* [Testing](testing.md)
1212
* [Documentation](documentation.md)
1313
* [Iterators](iterators.md)
1414
* [Concurrency](concurrency.md)
1515
* [Error Handling](error-handling.md)
1616
* [FFI](ffi.md)
17-
* [`Deref` coercions](deref-coercions.md)
17+
* [Deref coercions](deref-coercions.md)
1818
* [Syntax and Semantics](syntax-and-semantics.md)
1919
* [Variable Bindings](variable-bindings.md)
20-
* [Primitive Types](primitive-types.md)
2120
* [Functions](functions.md)
21+
* [Primitive Types](primitive-types.md)
2222
* [Comments](comments.md)
23-
* [Structs](structs.md)
24-
* [Mutability](mutability.md)
25-
* [Method Syntax](method-syntax.md)
26-
* [Enums](enums.md)
27-
* [`if`](if.md)
28-
* [Match](match.md)
29-
* [Patterns](patterns.md)
30-
* [`for` loops](for-loops.md)
31-
* [`while` loops](while-loops.md)
23+
* [if](if.md)
24+
* [for loops](for-loops.md)
25+
* [while loops](while-loops.md)
3226
* [Ownership](ownership.md)
3327
* [References and Borrowing](references-and-borrowing.md)
3428
* [Lifetimes](lifetimes.md)
29+
* [Mutability](mutability.md)
3530
* [Move semantics](move-semantics.md)
31+
* [Enums](enums.md)
32+
* [Match](match.md)
33+
* [Patterns](patterns.md)
34+
* [Structs](structs.md)
35+
* [Method Syntax](method-syntax.md)
3636
* [Drop](drop.md)
3737
* [Vectors](vectors.md)
38-
* [Arrays](arrays.md)
39-
* [Slices](slices.md)
4038
* [Strings](strings.md)
4139
* [Traits](traits.md)
4240
* [Operators and Overloading](operators-and-overloading.md)
4341
* [Generics](generics.md)
42+
* [if let](if-let.md)
4443
* [Trait Objects](trait-objects.md)
4544
* [Closures](closures.md)
4645
* [Universal Function Call Syntax](ufcs.md)
4746
* [Crates and Modules](crates-and-modules.md)
4847
* [`static`](static.md)
4948
* [`const`](const.md)
50-
* [Tuples](tuples.md)
5149
* [Tuple Structs](tuple-structs.md)
5250
* [Attributes](attributes.md)
5351
* [Conditional Compilation](conditional-compilation.md)
@@ -66,4 +64,6 @@
6664
* [Link args](link-args.md)
6765
* [Benchmark Tests](benchmark-tests.md)
6866
* [Box Syntax and Patterns](box-syntax-and-patterns.md)
67+
* [Slice Patterns](slice-patterns.md)
6968
* [Glossary](glossary.md)
69+
* [Academic Research](academic-research.md)

src/doc/trpl/academic-research.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
% Academic Research
2+
3+
An incomplete list of papers that have had some influence in Rust.
4+
5+
Recommended for inspiration and a better understanding of Rust's background.
6+
7+
### Type system
8+
9+
* [Region based memory management in Cyclone](http://209.68.42.137/ucsd-pages/Courses/cse227.w03/handouts/cyclone-regions.pdf)
10+
* [Safe manual memory management in Cyclone](http://www.cs.umd.edu/projects/PL/cyclone/scp.pdf)
11+
* [Typeclasses: making ad-hoc polymorphism less ad hoc](http://www.ps.uni-sb.de/courses/typen-ws99/class.ps.gz)
12+
* [Macros that work together](https://www.cs.utah.edu/plt/publications/jfp12-draft-fcdf.pdf)
13+
* [Traits: composable units of behavior](http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf)
14+
* [Alias burying](http://www.cs.uwm.edu/faculty/boyland/papers/unique-preprint.ps) - We tried something similar and abandoned it.
15+
* [External uniqueness is unique enough](http://www.computingscience.nl/research/techreps/repo/CS-2002/2002-048.pdf)
16+
* [Uniqueness and Reference Immutability for Safe Parallelism](https://research.microsoft.com/pubs/170528/msr-tr-2012-79.pdf)
17+
* [Region Based Memory Management](http://www.cs.ucla.edu/~palsberg/tba/papers/tofte-talpin-iandc97.pdf)
18+
19+
### Concurrency
20+
21+
* [Singularity: rethinking the software stack](https://research.microsoft.com/pubs/69431/osr2007_rethinkingsoftwarestack.pdf)
22+
* [Language support for fast and reliable message passing in singularity OS](https://research.microsoft.com/pubs/67482/singsharp.pdf)
23+
* [Scheduling multithreaded computations by work stealing](http://supertech.csail.mit.edu/papers/steal.pdf)
24+
* [Thread scheduling for multiprogramming multiprocessors](http://www.eecis.udel.edu/%7Ecavazos/cisc879-spring2008/papers/arora98thread.pdf)
25+
* [The data locality of work stealing](http://www.aladdin.cs.cmu.edu/papers/pdfs/y2000/locality_spaa00.pdf)
26+
* [Dynamic circular work stealing deque](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.170.1097&rep=rep1&type=pdf) - The Chase/Lev deque
27+
* [Work-first and help-first scheduling policies for async-finish task parallelism](http://www.cs.rice.edu/%7Eyguo/pubs/PID824943.pdf) - More general than fully-strict work stealing
28+
* [A Java fork/join calamity](http://www.coopsoft.com/ar/CalamityArticle.html) - critique of Java's fork/join library, particularly its application of work stealing to non-strict computation
29+
* [Scheduling techniques for concurrent systems](http://www.ece.rutgers.edu/%7Eparashar/Classes/ece572-papers/05/ps-ousterhout.pdf)
30+
* [Contention aware scheduling](http://www.blagodurov.net/files/a8-blagodurov.pdf)
31+
* [Balanced work stealing for time-sharing multicores](http://www.cse.ohio-state.edu/hpcs/WWW/HTML/publications/papers/TR-12-1.pdf)
32+
* [Three layer cake](http://www.upcrc.illinois.edu/workshops/paraplop10/papers/paraplop10_submission_8.pdf)
33+
* [Non-blocking steal-half work queues](http://www.cs.bgu.ac.il/%7Ehendlerd/papers/p280-hendler.pdf)
34+
* [Reagents: expressing and composing fine-grained concurrency](http://www.mpi-sws.org/~turon/reagents.pdf)
35+
* [Algorithms for scalable synchronization of shared-memory multiprocessors](https://www.cs.rochester.edu/u/scott/papers/1991_TOCS_synch.pdf)
36+
37+
### Others
38+
39+
* [Crash-only software](https://www.usenix.org/legacy/events/hotos03/tech/full_papers/candea/candea.pdf)
40+
* [Composing High-Performance Memory Allocators](http://people.cs.umass.edu/~emery/pubs/berger-pldi2001.pdf)
41+
* [Reconsidering Custom Memory Allocation](http://people.cs.umass.edu/~emery/pubs/berger-oopsla2002.pdf)
42+
43+
### Papers *about* Rust
44+
45+
* [GPU programming in Rust](http://www.cs.indiana.edu/~eholk/papers/hips2013.pdf)
46+
* [Parallel closures: a new twist on an old idea](https://www.usenix.org/conference/hotpar12/parallel-closures-new-twist-old-idea) - not exactly about rust, but by nmatsakis

src/doc/trpl/arrays.md

-48
This file was deleted.

src/doc/trpl/debug-and-display.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
% `Debug` and `Display`
1+
% Debug and Display
22

33
Coming soon!

src/doc/trpl/effective-rust.md

+7
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
% Effective Rust
2+
3+
So you’ve learned how to write some Rust code. But there’s a difference between
4+
writing *any* Rust code and writing *good* Rust code.
5+
6+
This section consists of relatively independent tutorials which show you how to
7+
take your Rust to the next level. Common patterns and standard library features
8+
will be introduced. Read these sections in any order of your choosing.

0 commit comments

Comments
 (0)