Skip to content

Commit 665aed0

Browse files
committed
Backport TRPL, reference, and grammar.
Rather than port each individual change to these files, for the release, I just waited to do it all at the end, in this commit. Since individual comits made it to master, everyone should get proper credit in the main tree, and AUTHORS includes those whose changes are here.
1 parent 6e2106d commit 665aed0

Some content is hidden

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

62 files changed

+6688
-2110
lines changed

src/doc/grammar.md

Lines changed: 117 additions & 93 deletions
Large diffs are not rendered by default.

src/doc/reference.md

Lines changed: 508 additions & 812 deletions
Large diffs are not rendered by default.

src/doc/trpl/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ good at: embedding in other languages, programs with specific space and time
88
requirements, and writing low-level code, like device drivers and operating
99
systems. It improves on current languages targeting this space by having a
1010
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
11+
eliminating all data races. Rust also aims to achieve ‘zero-cost abstractions
1212
even though some of these abstractions feel like those of a high-level
1313
language. Even then, Rust still allows precise control like a low-level
1414
language would.
@@ -24,20 +24,27 @@ is the first. After this:
2424
* [Syntax and Semantics][ss] - Each bit of Rust, broken down into small chunks.
2525
* [Nightly Rust][nr] - Cutting-edge features that aren’t in stable builds yet.
2626
* [Glossary][gl] - A reference of terms used in the book.
27+
* [Academic Research][ar] - Literature that influenced Rust.
2728

2829
[gs]: getting-started.html
2930
[lr]: learn-rust.html
3031
[er]: effective-rust.html
3132
[ss]: syntax-and-semantics.html
3233
[nr]: nightly-rust.html
3334
[gl]: glossary.html
35+
[ar]: academic-research.html
3436

3537
After reading this introduction, you’ll want to dive into either ‘Learn Rust’
3638
or ‘Syntax and Semantics’, depending on your preference: ‘Learn Rust’ if you
3739
want to dive in with a project, or ‘Syntax and Semantics’ if you prefer to
3840
start small, and learn a single concept thoroughly before moving onto the next.
3941
Copious cross-linking connects these parts together.
4042

43+
### Contributing
44+
45+
The source files from which this book is generated can be found on Github:
46+
[github.com/rust-lang/rust/tree/master/src/doc/trpl](https://github.com/rust-lang/rust/tree/master/src/doc/trpl)
47+
4148
## A brief introduction to Rust
4249

4350
Is Rust a language you might be interested in? Let’s examine a few small code
@@ -125,7 +132,7 @@ vector. When we try to compile this program, we get an error:
125132

126133
```text
127134
error: cannot borrow `x` as mutable because it is also borrowed as immutable
128-
x.push(4);
135+
x.push("foo");
129136
^
130137
note: previous borrow of `x` occurs here; the immutable borrow prevents
131138
subsequent moves or mutable borrows of `x` until the borrow ends
@@ -165,7 +172,7 @@ fn main() {
165172

166173
Rust has [move semantics][move] by default, so if we want to make a copy of some
167174
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
175+
to the vector stored in `x`, but a copy of its first element, `"Hello"`. Now
169176
that we don’t have a reference, our `push()` works just fine.
170177

171178
[move]: move-semantics.html
@@ -188,5 +195,5 @@ fn main() {
188195
We created an inner scope with an additional set of curly braces. `y` will go out of
189196
scope before we call `push()`, and so we’re all good.
190197

191-
This concept of ownership isn’t just good for preventing danging pointers, but an
198+
This concept of ownership isn’t just good for preventing dangling pointers, but an
192199
entire set of related problems, like iterator invalidation, concurrency, and more.

src/doc/trpl/SUMMARY.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
* [Hello, world!](hello-world.md)
66
* [Hello, Cargo!](hello-cargo.md)
77
* [Learn Rust](learn-rust.md)
8+
* [Guessing Game](guessing-game.md)
9+
* [Dining Philosophers](dining-philosophers.md)
10+
* [Rust inside other languages](rust-inside-other-languages.md)
811
* [Effective Rust](effective-rust.md)
912
* [The Stack and the Heap](the-stack-and-the-heap.md)
10-
* [Debug and Display](debug-and-display.md)
1113
* [Testing](testing.md)
14+
* [Conditional Compilation](conditional-compilation.md)
1215
* [Documentation](documentation.md)
1316
* [Iterators](iterators.md)
1417
* [Concurrency](concurrency.md)
1518
* [Error Handling](error-handling.md)
1619
* [FFI](ffi.md)
17-
* [Deref coercions](deref-coercions.md)
20+
* [Borrow and AsRef](borrow-and-asref.md)
21+
* [Release Channels](release-channels.md)
1822
* [Syntax and Semantics](syntax-and-semantics.md)
1923
* [Variable Bindings](variable-bindings.md)
2024
* [Functions](functions.md)
@@ -27,34 +31,32 @@
2731
* [References and Borrowing](references-and-borrowing.md)
2832
* [Lifetimes](lifetimes.md)
2933
* [Mutability](mutability.md)
30-
* [Move semantics](move-semantics.md)
34+
* [Structs](structs.md)
3135
* [Enums](enums.md)
3236
* [Match](match.md)
3337
* [Patterns](patterns.md)
34-
* [Structs](structs.md)
3538
* [Method Syntax](method-syntax.md)
36-
* [Drop](drop.md)
3739
* [Vectors](vectors.md)
3840
* [Strings](strings.md)
39-
* [Traits](traits.md)
40-
* [Operators and Overloading](operators-and-overloading.md)
4141
* [Generics](generics.md)
42+
* [Traits](traits.md)
43+
* [Drop](drop.md)
4244
* [if let](if-let.md)
4345
* [Trait Objects](trait-objects.md)
4446
* [Closures](closures.md)
4547
* [Universal Function Call Syntax](ufcs.md)
4648
* [Crates and Modules](crates-and-modules.md)
47-
* [`static`](static.md)
48-
* [`const`](const.md)
49-
* [Tuple Structs](tuple-structs.md)
49+
* [`const` and `static`](const-and-static.md)
5050
* [Attributes](attributes.md)
51-
* [Conditional Compilation](conditional-compilation.md)
5251
* [`type` aliases](type-aliases.md)
5352
* [Casting between types](casting-between-types.md)
5453
* [Associated Types](associated-types.md)
5554
* [Unsized Types](unsized-types.md)
55+
* [Operators and Overloading](operators-and-overloading.md)
56+
* [Deref coercions](deref-coercions.md)
5657
* [Macros](macros.md)
57-
* [`unsafe` Code](unsafe-code.md)
58+
* [Raw Pointers](raw-pointers.md)
59+
* [`unsafe`](unsafe.md)
5860
* [Nightly Rust](nightly-rust.md)
5961
* [Compiler Plugins](compiler-plugins.md)
6062
* [Inline Assembly](inline-assembly.md)
@@ -65,5 +67,6 @@
6567
* [Benchmark Tests](benchmark-tests.md)
6668
* [Box Syntax and Patterns](box-syntax-and-patterns.md)
6769
* [Slice Patterns](slice-patterns.md)
70+
* [Associated Constants](associated-constants.md)
6871
* [Glossary](glossary.md)
6972
* [Academic Research](academic-research.md)

src/doc/trpl/associated-constants.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
% Associated Constants
2+
3+
With the `associated_consts` feature, you can define constants like this:
4+
5+
```rust
6+
#![feature(associated_consts)]
7+
8+
trait Foo {
9+
const ID: i32;
10+
}
11+
12+
impl Foo for i32 {
13+
const ID: i32 = 1;
14+
}
15+
16+
fn main() {
17+
assert_eq!(1, i32::ID);
18+
}
19+
```
20+
21+
Any implementor of `Foo` will have to define `ID`. Without the definition:
22+
23+
```rust,ignore
24+
#![feature(associated_consts)]
25+
26+
trait Foo {
27+
const ID: i32;
28+
}
29+
30+
impl Foo for i32 {
31+
}
32+
```
33+
34+
gives
35+
36+
```text
37+
error: not all trait items implemented, missing: `ID` [E0046]
38+
impl Foo for i32 {
39+
}
40+
```
41+
42+
A default value can be implemented as well:
43+
44+
```rust
45+
#![feature(associated_consts)]
46+
47+
trait Foo {
48+
const ID: i32 = 1;
49+
}
50+
51+
impl Foo for i32 {
52+
}
53+
54+
impl Foo for i64 {
55+
const ID: i32 = 5;
56+
}
57+
58+
fn main() {
59+
assert_eq!(1, i32::ID);
60+
assert_eq!(5, i64::ID);
61+
}
62+
```
63+
64+
As you can see, when implementing `Foo`, you can leave it unimplemented, as
65+
with `i32`. It will then use the default value. But, as in `i64`, we can also
66+
add our own definition.
67+
68+
Associated constants don’t have to be associated with a trait. An `impl` block
69+
for a `struct` works fine too:
70+
71+
```rust
72+
#![feature(associated_consts)]
73+
74+
struct Foo;
75+
76+
impl Foo {
77+
pub const FOO: u32 = 3;
78+
}
79+
```

src/doc/trpl/associated-types.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
% Associated Types
22

3-
Associated types are a powerful part of Rust's type system. They're related to
4-
the idea of a 'type family', in other words, grouping multiple types together. That
5-
description is a bit abstract, so let's dive right into an example. If you want
3+
Associated types are a powerful part of Rusts type system. Theyre related to
4+
the idea of a type family, in other words, grouping multiple types together. That
5+
description is a bit abstract, so lets dive right into an example. If you want
66
to write a `Graph` trait, you have two types to be generic over: the node type
77
and the edge type. So you might write a trait, `Graph<N, E>`, that looks like
88
this:
@@ -48,11 +48,11 @@ fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> uint { ... }
4848

4949
No need to deal with the `E`dge type here!
5050

51-
Let's go over all this in more detail.
51+
Lets go over all this in more detail.
5252

5353
## Defining associated types
5454

55-
Let's build that `Graph` trait. Here's the definition:
55+
Lets build that `Graph` trait. Heres the definition:
5656

5757
```rust
5858
trait Graph {
@@ -86,7 +86,7 @@ trait Graph {
8686
## Implementing associated types
8787

8888
Just like any trait, traits that use associated types use the `impl` keyword to
89-
provide implementations. Here's a simple implementation of Graph:
89+
provide implementations. Heres a simple implementation of Graph:
9090

9191
```rust
9292
# trait Graph {
@@ -118,13 +118,13 @@ impl Graph for MyGraph {
118118
This silly implementation always returns `true` and an empty `Vec<Edge>`, but it
119119
gives you an idea of how to implement this kind of thing. We first need three
120120
`struct`s, one for the graph, one for the node, and one for the edge. If it made
121-
more sense to use a different type, that would work as well, we're just going to
121+
more sense to use a different type, that would work as well, were just going to
122122
use `struct`s for all three here.
123123

124124
Next is the `impl` line, which is just like implementing any other trait.
125125

126126
From here, we use `=` to define our associated types. The name the trait uses
127-
goes on the left of the `=`, and the concrete type we're `impl`ementing this
127+
goes on the left of the `=`, and the concrete type were `impl`ementing this
128128
for goes on the right. Finally, we use the concrete types in our function
129129
declarations.
130130

src/doc/trpl/attributes.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
11
% Attributes
22

3-
Coming Soon!
3+
Declarations can be annotated with ‘attributes’ in Rust. They look like this:
4+
5+
```rust
6+
#[test]
7+
# fn foo() {}
8+
```
9+
10+
or like this:
11+
12+
```rust
13+
# mod foo {
14+
#![test]
15+
# }
16+
```
17+
18+
The difference between the two is the `!`, which changes what the attribute
19+
applies to:
20+
21+
```rust,ignore
22+
#[foo]
23+
struct Foo;
24+
25+
mod bar {
26+
#![bar]
27+
}
28+
```
29+
30+
The `#[foo]` attribute applies to the next item, which is the `struct`
31+
declaration. The `#![bar]` attribute applies to the item enclosing it, which is
32+
the `mod` declaration. Otherwise, they’re the same. Both change the meaning of
33+
the item they’re attached to somehow.
34+
35+
For example, consider a function like this:
36+
37+
```rust
38+
#[test]
39+
fn check() {
40+
assert_eq!(2, 1 + 1);
41+
}
42+
```
43+
44+
It is marked with `#[test]`. This means it’s special: when you run
45+
[tests][tests], this function will execute. When you compile as usual, it won’t
46+
even be included. This function is now a test function.
47+
48+
[tests]: testing.html
49+
50+
Attributes may also have additional data:
51+
52+
```rust
53+
#[inline(always)]
54+
fn super_fast_fn() {
55+
# }
56+
```
57+
58+
Or even keys and values:
59+
60+
```rust
61+
#[cfg(target_os = "macos")]
62+
mod macos_only {
63+
# }
64+
```
65+
66+
Rust attributes are used for a number of different things. There is a full list
67+
of attributes [in the reference][reference]. Currently, you are not allowed to
68+
create your own attributes, the Rust compiler defines them.
69+
70+
[reference]: ../reference.html#attributes

src/doc/trpl/benchmark-tests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn add_two(a: i32) -> i32 {
1313
}
1414
1515
#[cfg(test)]
16-
mod test {
16+
mod tests {
1717
use super::*;
1818
use test::Bencher;
1919

0 commit comments

Comments
 (0)