Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 51 additions & 10 deletions second-edition/src/appendix-02-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ trait bounds, macros, attributes, comments, tuples, and brackets.

### Operators

Table B-1 contains the operators in Rust, an example of how the operator would
[Table B-1][Table-B-1] contains the operators in Rust, an example of how the operator would
appear in context, a short explanation, and whether that operator is
overloadable. If an operator is overloadable, the relevant trait to use to
overload that operator is listed.

[Table-B-1]: #Table-B-1
<a name="Table-B-1"></a>

<span class="caption">Table B-1: Operators</span>

| Operator | Example | Explanation | Overloadable? |
Expand Down Expand Up @@ -74,9 +77,12 @@ overload that operator is listed.
The following list contains all non-letters that don’t function as operators;
that is, they don’t behave like a function or method call.

Table B-2 shows symbols that appear on their own and are valid in a variety of
[Table B-2][Table-B-2] shows symbols that appear on their own and are valid in a variety of
locations.

[Table-B-2]: #Table-B-2
<a name="Table-B-2"></a>

<span class="caption">Table B-2: Stand-Alone Syntax</span>

| Symbol | Explanation |
Expand All @@ -93,9 +99,12 @@ locations.
| `!` | Always empty bottom type for diverging functions |
| `_` | “Ignored” pattern binding; also used to make integer literals readable |

Table B-3 shows symbols that appear in the context of a path through the module
[Table B-3][Table-B-3] shows symbols that appear in the context of a path through the module
hierarchy to an item.

[Table-B-3]: #Table-B-3
<a name="Table-B-3"></a>

<span class="caption">Table B-3: Path-Related Syntax</span>

| Symbol | Explanation |
Expand All @@ -110,9 +119,12 @@ hierarchy to an item.
| `type::method(...)` | Disambiguating a method call by naming the type for which it’s defined |
| `<type as trait>::method(...)` | Disambiguating a method call by naming the trait and type |

Table B-4 shows symbols that appear in the context of using generic type
[Table B-4][Table-B-4] shows symbols that appear in the context of using generic type
parameters.

[Table-B-4]: #Table-B-4
<a name="Table-B-4"></a>

<span class="caption">Table B-4: Generics</span>

| Symbol | Explanation |
Expand All @@ -126,9 +138,12 @@ parameters.
| `for<...> type` | Higher-ranked lifetime bounds |
| `type<ident=type>` | A generic type where one or more associated types have specific assignments (e.g., `Iterator<Item=T>`) |

Table B-5 shows symbols that appear in the context of constraining generic type
[Table B-5][Table-B-5] shows symbols that appear in the context of constraining generic type
parameters with trait bounds.

[Table-B-5]: #Table-B-5
<a name="Table-B-5"></a>

<span class="caption">Table B-5: Trait Bound Constraints</span>

| Symbol | Explanation |
Expand All @@ -140,9 +155,12 @@ parameters with trait bounds.
| `T: ?Sized` | Allow generic type parameter to be a dynamically sized type |
| `'a + trait`, `trait + trait` | Compound type constraint |

Table B-6 shows symbols that appear in the context of calling or defining
[Table B-6][Table-B-6] shows symbols that appear in the context of calling or defining
macros and specifying attributes on an item.

[Table-B-6]: #Table-B-6
<a name="Table-B-6"></a>

<span class="caption">Table B-6: Macros and Attributes</span>

| Symbol | Explanation |
Expand All @@ -153,7 +171,10 @@ macros and specifying attributes on an item.
| `$ident:kind` | Macro capture |
| `$(…)…` | Macro repetition |

Table B-7 shows symbols that create comments.
[Table B-7][Table-B-7] shows symbols that create comments.

[Table-B-7]: #Table-B-7
<a name="Table-B-7"></a>

<span class="caption">Table B-7: Comments</span>

Expand All @@ -166,7 +187,10 @@ Table B-7 shows symbols that create comments.
| `/*!...*/` | Inner block doc comment |
| `/**...*/` | Outer block doc comment |

Table B-8 shows symbols that appear in the context of using tuples.
[Table B-8][Table-B-8] shows symbols that appear in the context of using tuples.

[Table-B-8]: #Table-B-8
<a name="Table-B-8"></a>

<span class="caption">Table B-8: Tuples</span>

Expand All @@ -182,7 +206,10 @@ Table B-8 shows symbols that appear in the context of using tuples.
| `ident!(...)`, `ident!{...}`, `ident![...]` | Macro invocation |
| `expr.0`, `expr.1`, etc. | Tuple indexing |

Table B-9 shows the contexts in which curly braces are used.
[Table B-9][Table-B-9] shows the contexts in which curly braces are used.

[Table-B-9]: #Table-B-9
<a name="Table-B-9"></a>

<span class="caption">Table B-9: Curly Brackets</span>

Expand All @@ -191,7 +218,10 @@ Table B-9 shows the contexts in which curly braces are used.
| `{...}` | Block expression |
| `Type {...}` | `struct` literal |

Table B-10 shows the contexts in which square brackets are used.
[Table B-10][Table-B-10] shows the contexts in which square brackets are used.

[Table-B-10]: #Table-B-10
<a name="Table-B-10"></a>

<span class="caption">Table B-10: Square Brackets</span>

Expand All @@ -202,3 +232,14 @@ Table B-10 shows the contexts in which square brackets are used.
| `[type; len]` | Array type containing `len` instances of `type` |
| `expr[expr]` | Collection indexing. Overloadable (`Index`, `IndexMut`) |
| `expr[..]`, `expr[a..]`, `expr[..b]`, `expr[a..b]` | Collection indexing pretending to be collection slicing, using `Range`, `RangeFrom`, `RangeTo`, or `RangeFull` as the “index” |

[Table-B-1]: appendix-02-operators.html#Table-B-1
[Table-B-2]: appendix-02-operators.html#Table-B-2
[Table-B-3]: appendix-02-operators.html#Table-B-3
[Table-B-4]: appendix-02-operators.html#Table-B-4
[Table-B-5]: appendix-02-operators.html#Table-B-5
[Table-B-6]: appendix-02-operators.html#Table-B-6
[Table-B-7]: appendix-02-operators.html#Table-B-7
[Table-B-8]: appendix-02-operators.html#Table-B-8
[Table-B-9]: appendix-02-operators.html#Table-B-9
[Table-B-10]: appendix-02-operators.html#Table-B-10
30 changes: 21 additions & 9 deletions second-edition/src/appendix-04-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ We could also use the `vec!` macro to make a vector of two integers or a vector
of five string slices. We wouldn’t be able to use a function to do the same
because we wouldn’t know the number or type of values up front.

Let’s look at a slightly simplified definition of the `vec!` macro in Listing
D-1.
Let’s look at a slightly simplified definition of the `vec!` macro in [Listing D-1][Listing-D-1].

[Listing-D-1]: #Listing-D-1
<a name="Listing-D-1"></a>

```rust
#[macro_export]
Expand Down Expand Up @@ -137,7 +139,7 @@ other will be an error. More complex macros will have more than one arm.
Valid pattern syntax in macro definitions is different than the pattern syntax
covered in Chapter 18 because macro patterns are matched against Rust code
structure rather than values. Let’s walk through what the pieces of the pattern
in Listing D-1 mean; for the full macro pattern syntax, see [the reference].
in [Listing D-1][Listing-D-1] mean; for the full macro pattern syntax, see [the reference].

[the reference]: ../../reference/macros.html

Expand Down Expand Up @@ -198,10 +200,13 @@ types, we’ll provide a procedural macro so users can annotate their type with
function. The default implementation will print `Hello, Macro! My name is
TypeName!` where `TypeName` is the name of the type on which this trait has
been defined. In other words, we’ll write a crate that enables another
programmer to write code like Listing D-2 using our crate.
programmer to write code like [Listing D-2][Listing-D-2] using our crate.

<span class="filename">Filename: src/main.rs</span>

[Listing-D-2]: #Listing-D-2
<a name="Listing-D-2"></a>

```rust,ignore
extern crate hello_macro;
#[macro_use]
Expand Down Expand Up @@ -305,12 +310,15 @@ syn = "0.11.11"
quote = "0.3.15"
```

To start defining the procedural macro, place the code in Listing D-3 into your
To start defining the procedural macro, place the code in [Listing D-3][Listing-D-3] into your
*src/lib.rs* file for the `hello_macro_derive` crate. Note that this code won’t
compile until we add a definition for the `impl_hello_macro` function.

<span class="filename">Filename: hello_macro_derive/src/lib.rs</span>

[Listing-D-3]: #Listing-D-3
<a name="Listing-D-3"></a>

```rust,ignore
extern crate proc_macro;
extern crate syn;
Expand Down Expand Up @@ -364,7 +372,7 @@ most procedural macros follow.

This function first converts the `input` from a `TokenStream` to a `String` by
calling `to_string`. This `String` is a string representation of the Rust code
for which we are deriving `HelloMacro`. In the example in Listing D-2, `s` will
for which we are deriving `HelloMacro`. In the example in [Listing D-2][Listing-D-2], `s` will
have the `String` value `struct Pancakes;` because that is the Rust code we
added the `#[derive(HelloMacro)]` annotation to.

Expand Down Expand Up @@ -434,7 +442,7 @@ fn impl_hello_macro(ast: &syn::DeriveInput) -> quote::Tokens {
```

We get an `Ident` struct instance containing the name (identifier) of the
annotated type using `ast.ident`. The code in Listing D-2 specifies that the
annotated type using `ast.ident`. The code in [Listing D-2][Listing-D-2] specifies that the
`name` will be `Ident("Pancakes")`.

The `quote!` macro lets us write the Rust code that we want to return and
Expand All @@ -461,7 +469,7 @@ to print literally, so we use `stringify!`. Using `stringify!` also saves an
allocation by converting `#name` to a string literal at compile time.

At this point, `cargo build` should complete successfully in both `hello_macro`
and `hello_macro_derive`. Let’s hook up these crates to the code in Listing D-2
and `hello_macro_derive`. Let’s hook up these crates to the code in [Listing D-2][Listing-D-2]
to see the procedural macro in action! Create a new binary project in your
*projects* directory using `cargo new --bin pancakes`. We need to add
`hello_macro` and `hello_macro_derive` as dependencies in the `pancakes`
Expand All @@ -475,7 +483,7 @@ hello_macro = { path = "../hello_macro" }
hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }
```

Put the code from Listing D-2 into *src/main.rs*, and run `cargo run`: it
Put the code from [Listing D-2][Listing-D-2] into *src/main.rs*, and run `cargo run`: it
should print `Hello, Macro! My name is Pancakes!` The implementation of the
`HelloMacro` trait from the procedural macro was included without the
`pancakes` crate needing to implement it; the `#[derive(HelloMacro)]` added the
Expand All @@ -488,3 +496,7 @@ use a better declarative macro system with the `macro` keyword and will add
more types of procedural macros for more powerful tasks than just `derive`.
These systems are still under development at the time of this publication;
please consult the online Rust documentation for the latest information.

[Listing-D-1]: appendix-04-macros.html#Listing-D-1
[Listing-D-2]: appendix-04-macros.html#Listing-D-2
[Listing-D-3]: appendix-04-macros.html#Listing-D-3
7 changes: 6 additions & 1 deletion second-edition/src/ch01-02-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ the *.rs* extension. If you’re using more than one word in your filename, use
an underscore to separate them. For example, use *hello_world.rs* rather than
*helloworld.rs*.

Now open the *main.rs* file you just created and enter the code in Listing 1-1.
Now open the *main.rs* file you just created and enter the code in [Listing 1-1][Listing-1-1].

<span class="filename">Filename: main.rs</span>

[Listing-1-1]: #Listing-1-1
<a name="Listing-1-1"></a>

```rust
fn main() {
println!("Hello, world!");
Expand Down Expand Up @@ -200,3 +203,5 @@ Just compiling with `rustc` is fine for simple programs, but as your project
grows, you’ll want to manage all the options and make it easy to share your
code. Next, we’ll introduce you to the Cargo tool, which will help you write
real-world Rust programs.

[Listing-1-1]: ch01-02-hello-world.html#Listing-1-1
10 changes: 8 additions & 2 deletions second-edition/src/ch01-03-hello-cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ repository along with a *.gitignore* file.
> the `--vcs` flag. Run `cargo new --help` to see the available options.

Open *Cargo.toml* in your text editor of choice. It should look similar to the
code in Listing 1-2.
code in [Listing 1-2][Listing-1-2].

<span class="filename">Filename: Cargo.toml</span>

[Listing-1-2]: #Listing-1-2
<a name="Listing-1-2"></a>

```toml
[package]
name = "hello_cargo"
Expand Down Expand Up @@ -98,7 +101,7 @@ fn main() {
```

Cargo has generated a Hello, world! program for you, just like the one we wrote
in Listing 1-1! So far, the differences between our previous project and the
in [Listing 1-1][Listing-1-1]! So far, the differences between our previous project and the
project Cargo generates are that Cargo placed the code in the *src* directory,
and we have a *Cargo.toml* configuration file in the top directory.

Expand Down Expand Up @@ -242,3 +245,6 @@ This is a great time to build a more substantial program to get used to reading
and writing Rust code. So, in Chapter 2, we’ll build a guessing game program.
If you would rather start by learning how common programming concepts work in
Rust, see Chapter 3 and then return to Chapter 2.

[Listing-1-1]: ch01-02-hello-world.html#Listing-1-1
[Listing-1-2]: ch01-03-hello-cargo.html#Listing-1-2
Loading