Skip to content

Commit cb42847

Browse files
committed
---
yaml --- r: 185981 b: refs/heads/auto c: ce5f1b3 h: refs/heads/master i: 185979: f67c23a v: v3
1 parent 501d61c commit cb42847

File tree

186 files changed

+2850
-1254
lines changed

Some content is hidden

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

186 files changed

+2850
-1254
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 07dc8d67c92017f950eef3951ec901cb2a3add7e
13+
refs/heads/auto: ce5f1b3216be790be590ad71eabccc16c9b86c74
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/doc/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ There are questions that are asked quite often, and so we've made FAQs for them:
6868
* [Language Design FAQ](complement-design-faq.html)
6969
* [Language FAQ](complement-lang-faq.html)
7070
* [Project FAQ](complement-project-faq.html)
71-
* [How to submit a bug report](complement-bugreport.html)
71+
* [How to submit a bug report](https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports)
7272

7373
# The standard library
7474

branches/auto/src/doc/reference.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ nonzero_dec: '1' | '2' | '3' | '4'
302302

303303
A _character literal_ is a single Unicode character enclosed within two
304304
`U+0027` (single-quote) characters, with the exception of `U+0027` itself,
305-
which must be _escaped_ by a preceding U+005C character (`\`).
305+
which must be _escaped_ by a preceding `U+005C` character (`\`).
306306

307307
##### String literals
308308

@@ -311,6 +311,19 @@ A _string literal_ is a sequence of any Unicode characters enclosed within two
311311
which must be _escaped_ by a preceding `U+005C` character (`\`), or a _raw
312312
string literal_.
313313

314+
A multi-line string literal may be defined by terminating each line with a
315+
`U+005C` character (`\`) immediately before the newline. This causes the
316+
`U+005C` character, the newline, and all whitespace at the beginning of the
317+
next line to be ignored.
318+
319+
```rust
320+
let a = "foobar";
321+
let b = "foo\
322+
bar";
323+
324+
assert_eq!(a,b);
325+
```
326+
314327
##### Character escapes
315328

316329
Some additional _escapes_ are available in either character or non-raw string
@@ -731,15 +744,20 @@ Rust syntax is restricted in two ways:
731744
pairs when they occur at the beginning of, or immediately after, a `$(...)*`;
732745
requiring a distinctive token in front can solve the problem.
733746

734-
## Syntax extensions useful for the macro author
747+
## Syntax extensions useful in macros
735748

736-
* `log_syntax!` : print out the arguments at compile time
737-
* `trace_macros!` : supply `true` or `false` to enable or disable macro expansion logging
738749
* `stringify!` : turn the identifier argument into a string literal
739750
* `concat!` : concatenates a comma-separated list of literals
740-
* `concat_idents!` : create a new identifier by concatenating the arguments
741751

742-
The following attributes are used for quasiquoting in procedural macros:
752+
## Syntax extensions for macro debugging
753+
754+
* `log_syntax!` : print out the arguments at compile time
755+
* `trace_macros!` : supply `true` or `false` to enable or disable macro expansion logging
756+
757+
## Quasiquoting
758+
759+
The following syntax extensions are used for quasiquoting Rust syntax trees,
760+
usually in [procedural macros](book/plugins.html#syntax-extensions):
743761

744762
* `quote_expr!`
745763
* `quote_item!`
@@ -748,6 +766,8 @@ The following attributes are used for quasiquoting in procedural macros:
748766
* `quote_tokens!`
749767
* `quote_ty!`
750768

769+
Documentation is very limited at the moment.
770+
751771
# Crates and source files
752772

753773
Rust is a *compiled* language. Its semantics obey a *phase distinction*

branches/auto/src/doc/trpl/advanced-macros.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,58 @@ To keep this system simple and correct, `#[macro_use] extern crate ...` may
192192
only appear at the root of your crate, not inside `mod`. This ensures that
193193
`$crate` is a single identifier.
194194

195-
# A final note
195+
# The deep end
196196

197-
Macros, as currently implemented, are not for the faint of heart. Even
198-
ordinary syntax errors can be more difficult to debug when they occur inside a
199-
macro, and errors caused by parse problems in generated code can be very
200-
tricky. Invoking the `log_syntax!` macro can help elucidate intermediate
201-
states, invoking `trace_macros!(true)` will automatically print those
202-
intermediate states out, and passing the flag `--pretty expanded` as a
203-
command-line argument to the compiler will show the result of expansion.
197+
The introductory chapter mentioned recursive macros, but it did not give the
198+
full story. Recursive macros are useful for another reason: Each recursive
199+
invocation gives you another opportunity to pattern-match the macro's
200+
arguments.
201+
202+
As an extreme example, it is possible, though hardly advisable, to implement
203+
the [Bitwise Cyclic Tag](http://esolangs.org/wiki/Bitwise_Cyclic_Tag) automaton
204+
within Rust's macro system.
205+
206+
```rust
207+
#![feature(trace_macros)]
208+
209+
macro_rules! bct {
210+
// cmd 0: d ... => ...
211+
(0, $($ps:tt),* ; $_d:tt)
212+
=> (bct!($($ps),*, 0 ; ));
213+
(0, $($ps:tt),* ; $_d:tt, $($ds:tt),*)
214+
=> (bct!($($ps),*, 0 ; $($ds),*));
215+
216+
// cmd 1p: 1 ... => 1 ... p
217+
(1, $p:tt, $($ps:tt),* ; 1)
218+
=> (bct!($($ps),*, 1, $p ; 1, $p));
219+
(1, $p:tt, $($ps:tt),* ; 1, $($ds:tt),*)
220+
=> (bct!($($ps),*, 1, $p ; 1, $($ds),*, $p));
221+
222+
// cmd 1p: 0 ... => 0 ...
223+
(1, $p:tt, $($ps:tt),* ; $($ds:tt),*)
224+
=> (bct!($($ps),*, 1, $p ; $($ds),*));
225+
226+
// halt on empty data string
227+
( $($ps:tt),* ; )
228+
=> (());
229+
}
230+
231+
fn main() {
232+
trace_macros!(true);
233+
# /* just check the definition
234+
bct!(0, 0, 1, 1, 1 ; 1, 0, 1);
235+
# */
236+
}
237+
```
238+
239+
Exercise: use macros to reduce duplication in the above definition of the
240+
`bct!` macro.
241+
242+
# Procedural macros
204243

205244
If Rust's macro system can't do what you need, you may want to write a
206245
[compiler plugin](plugins.html) instead. Compared to `macro_rules!`
207246
macros, this is significantly more work, the interfaces are much less stable,
208-
and the warnings about debugging apply ten-fold. In exchange you get the
247+
and bugs can be much harder to track down. In exchange you get the
209248
flexibility of running arbitrary Rust code within the compiler. Syntax
210249
extension plugins are sometimes called *procedural macros* for this reason.

branches/auto/src/doc/trpl/error-handling.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,78 @@ let input = io::stdin().read_line()
223223
.ok()
224224
.expect("Failed to read line");
225225
```
226+
226227
`ok()` converts the `IoResult` into an `Option`, and `expect()` does the same
227228
thing as `unwrap()`, but takes a message. This message is passed along to the
228229
underlying `panic!`, providing a better error message if the code errors.
230+
231+
# Using `try!`
232+
233+
When writing code that calls many functions that return the `Result` type, the
234+
error handling can be tedious. The `try!` macro hides some of the boilerplate
235+
of propagating errors up the call stack.
236+
237+
It replaces this:
238+
239+
```rust
240+
use std::fs::File;
241+
use std::io;
242+
use std::io::prelude::*;
243+
244+
struct Info {
245+
name: String,
246+
age: i32,
247+
rating: i32,
248+
}
249+
250+
fn write_info(info: &Info) -> io::Result<()> {
251+
let mut file = File::open("my_best_friends.txt").unwrap();
252+
253+
if let Err(e) = writeln!(&mut file, "name: {}", info.name) {
254+
return Err(e)
255+
}
256+
if let Err(e) = writeln!(&mut file, "age: {}", info.age) {
257+
return Err(e)
258+
}
259+
if let Err(e) = writeln!(&mut file, "rating: {}", info.rating) {
260+
return Err(e)
261+
}
262+
263+
return Ok(());
264+
}
265+
```
266+
267+
With this:
268+
269+
```rust
270+
use std::fs::File;
271+
use std::io;
272+
use std::io::prelude::*;
273+
274+
struct Info {
275+
name: String,
276+
age: i32,
277+
rating: i32,
278+
}
279+
280+
fn write_info(info: &Info) -> io::Result<()> {
281+
let mut file = try!(File::open("my_best_friends.txt"));
282+
283+
try!(writeln!(&mut file, "name: {}", info.name));
284+
try!(writeln!(&mut file, "age: {}", info.age));
285+
try!(writeln!(&mut file, "rating: {}", info.rating));
286+
287+
return Ok(());
288+
}
289+
```
290+
291+
Wrapping an expression in `try!` will result in the unwrapped success (`Ok`)
292+
value, unless the result is `Err`, in which case `Err` is returned early from
293+
the enclosing function.
294+
295+
It's worth noting that you can only use `try!` from a function that returns a
296+
`Result`, which means that you cannot use `try!` inside of `main()`, because
297+
`main()` doesn't return anything.
298+
299+
`try!` makes use of [`FromError`](../std/error/#the-fromerror-trait) to determine
300+
what to return in the error case.

branches/auto/src/doc/trpl/hello-cargo.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ the Cargo
1818
README](https://github.com/rust-lang/cargo#installing-cargo-from-nightlies)
1919
for specific instructions about installing it.
2020

21+
## Converting to Cargo
22+
2123
Let's convert Hello World to Cargo.
2224

2325
To Cargo-ify our project, we need to do two things: Make a `Cargo.toml`
2426
configuration file, and put our source file in the right place. Let's
2527
do that part first:
2628

27-
```{bash}
29+
```bash
2830
$ mkdir src
2931
$ mv main.rs src/main.rs
3032
```
@@ -36,7 +38,7 @@ place for everything, and everything in its place.
3638

3739
Next, our configuration file:
3840

39-
```{bash}
41+
```bash
4042
$ editor Cargo.toml
4143
```
4244

@@ -73,7 +75,7 @@ well as what it is named.
7375

7476
Once you have this file in place, we should be ready to build! Try this:
7577

76-
```{bash}
78+
```bash
7779
$ cargo build
7880
Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
7981
$ ./target/hello_world
@@ -103,6 +105,62 @@ That's it! We've successfully built `hello_world` with Cargo. Even though our
103105
program is simple, it's using much of the real tooling that you'll use for the
104106
rest of your Rust career.
105107

108+
## A New Project
109+
110+
You don't have to go through this whole process every time you want to start a new
111+
project! Cargo has the ability to make a bare-bones project directory in which you
112+
can start developing right away.
113+
114+
To start a new project with Cargo, use `cargo new`:
115+
116+
```bash
117+
$ cargo new hello_world --bin
118+
```
119+
120+
We're passing `--bin` because we're making a binary program: if we
121+
were making a library, we'd leave it off.
122+
123+
Let's check out what Cargo has generated for us:
124+
125+
```bash
126+
$ cd hello_world
127+
$ tree .
128+
.
129+
├── Cargo.toml
130+
└── src
131+
└── main.rs
132+
133+
1 directory, 2 files
134+
```
135+
136+
If you don't have the `tree` command, you can probably get it from your distro's package
137+
manager. It's not necessary, but it's certainly useful.
138+
139+
This is all we need to get started. First, let's check out `Cargo.toml`:
140+
141+
```toml
142+
[package]
143+
144+
name = "hello_world"
145+
version = "0.0.1"
146+
authors = ["Your Name <[email protected]>"]
147+
```
148+
149+
Cargo has populated this file with reasonable defaults based off the arguments you gave
150+
it and your `git` global configuration. You may notice that Cargo has also initialized
151+
the `hello_world` directory as a `git` repository.
152+
153+
Here's what's in `src/main.rs`:
154+
155+
```rust
156+
fn main() {
157+
println!("Hello, world!");
158+
}
159+
```
160+
161+
Cargo has generated a "Hello World!" for us, and you're ready to start coding! A
162+
much more in-depth guide to Cargo can be found [here](http://doc.crates.io/guide.html).
163+
106164
Now that you've got the tools down, let's actually learn more about the Rust
107165
language itself. These are the basics that will serve you well through the rest
108-
of your time with Rust.
166+
of your time with Rust.

0 commit comments

Comments
 (0)