-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
fix doc #24837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix doc #24837
Changes from 4 commits
2851702
9ba6924
ae5f43f
2eb61d4
4550f38
a233703
2e198dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| % Enums | ||
|
|
||
| Rust has a ‘sum type’, an `enum`. Enums are an incredibly useful feature of | ||
| Rust has a‘sum type’, an `enum`. Enums are an incredibly useful feature of | ||
| Rust, and are used throughout the standard library. An `enum` is a type which | ||
| relates a set of alternates to a specific name. For example, below we define | ||
| `Character` to be either a `Digit` or something else. | ||
|
|
@@ -28,6 +28,16 @@ In `Character`, for instance, `Digit` gives a meaningful name for an `i32` | |
| value, where `Other` is only a name. However, the fact that they represent | ||
| distinct categories of `Character` is a very useful property. | ||
|
|
||
| We use the `::` syntax to use the name of each variant: They’re scoped by the name | ||
| of the `enum` itself. This allows both of these to work: | ||
|
|
||
| ```rust,ignore | ||
| Character::Digit(10); | ||
| Hand::Digit; | ||
| ``` | ||
|
|
||
| Both variants are named `Digit`, but they’re scoped to the `enum` name. | ||
|
|
||
|
||
| The variants of an `enum` by default are not comparable with equality operators | ||
| (`==`, `!=`), have no ordering (`<`, `>=`, etc.), and do not support other | ||
| binary operations such as `*` and `+`. As such, the following code is invalid | ||
|
|
@@ -48,16 +58,6 @@ let four_is_smaller = four <= ten; | |
| let four_equals_ten = four == ten; | ||
| ``` | ||
|
|
||
| We use the `::` syntax to use the name of each variant: They’re scoped by the name | ||
| of the `enum` itself. This allows both of these to work: | ||
|
|
||
| ```rust,ignore | ||
| Character::Digit(10); | ||
| Hand::Digit; | ||
| ``` | ||
|
|
||
| Both variants are named `Digit`, but since they’re scoped to the `enum` name, | ||
|
|
||
| Not supporting these operations may seem rather limiting, but it’s a limitation | ||
| which we can overcome. There are two ways: by implementing equality ourselves, | ||
| or by pattern matching variants with [`match`][match] expressions, which you’ll | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ or cumbersome to express that pattern as a generic function, a trait, or | |
| anything else within Rust’s semantics. | ||
|
|
||
| Macros allow us to abstract at a syntactic level. A macro invocation is | ||
| shorthand for an "expanded" syntactic form. This expansion happens early in | ||
| shorthand for an‘expanded’syntactic form. This expansion happens early in | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, is it okay to delete the space after (I see more of these below; I'm going to stop reviewing now and let you address this first, since some of them seem like they are just making the diff noisy.) |
||
| compilation, before any static checking. As a result, macros can capture many | ||
| patterns of code reuse that Rust’s core abstractions cannot. | ||
|
|
||
|
|
@@ -23,7 +23,7 @@ difficult to design a well-behaved macro! Additionally, compiler errors in | |
| macro code are harder to interpret, because they describe problems in the | ||
| expanded code, not the source-level form that developers use. | ||
|
|
||
| These drawbacks make macros something of a "feature of last resort". That’s not | ||
| These drawbacks make macros something of a‘feature of last resort’. That’s not | ||
| to say that macros are bad; they are part of Rust because sometimes they’re | ||
| needed for truly concise, well-abstracted code. Just keep this tradeoff in | ||
| mind. | ||
|
|
@@ -98,7 +98,7 @@ cases. Above, we had | |
|
|
||
| This is like a `match` expression arm, but the matching happens on Rust syntax | ||
| trees, at compile time. The semicolon is optional on the last (here, only) | ||
| case. The "pattern" on the left-hand side of `=>` is known as a ‘matcher’. | ||
| case. The‘pattern’on the left-hand side of `=>` is known as a ‘matcher’. | ||
| These have [their own little grammar] within the language. | ||
|
|
||
| [their own little grammar]: ../reference.html#macros | ||
|
|
@@ -154,7 +154,7 @@ $( | |
| ``` | ||
|
|
||
| Each matched expression `$x` will produce a single `push` statement in the | ||
| macro expansion. The repetition in the expansion proceeds in "lockstep" with | ||
| macro expansion. The repetition in the expansion proceeds in‘lockstep’with | ||
| repetition in the matcher (more on this in a moment). | ||
|
|
||
| Because `$x` was already declared as matching an expression, we don’t repeat | ||
|
|
@@ -190,7 +190,7 @@ shorthand for a data type could be valid as either an expression or a pattern. | |
|
|
||
| The repetition operator follows two principal rules: | ||
|
|
||
| 1. `$(...)*` walks through one "layer" of repetitions, for all of the `$name`s | ||
| 1. `$(...)*` walks through one‘layer’of repetitions, for all of the `$name`s | ||
| it contains, in lockstep, and | ||
| 2. each `$name` must be under at least as many `$(...)*`s as it was matched | ||
| against. If it is under more, it’ll be duplicated, as appropriate. | ||
|
|
@@ -219,12 +219,12 @@ fn main() { | |
| ``` | ||
|
|
||
| That’s most of the matcher syntax. These examples use `$(...)*`, which is a | ||
| "zero or more" match. Alternatively you can write `$(...)+` for a "one or | ||
| more" match. Both forms optionally include a separator, which can be any token | ||
| ‘zero or more’match. Alternatively you can write `$(...)+` for a‘one or | ||
| more’match. Both forms optionally include a separator, which can be any token | ||
| except `+` or `*`. | ||
|
|
||
| This system is based on | ||
| "[Macro-by-Example](http://www.cs.indiana.edu/ftp/techreports/TR206.pdf)" | ||
| ‘[Macro-by-Example](http://www.cs.indiana.edu/ftp/techreports/TR206.pdf)’ | ||
| (PDF link). | ||
|
|
||
| # Hygiene | ||
|
|
@@ -316,7 +316,7 @@ fn main() { | |
| This works because Rust has a [hygienic macro system][]. Each macro expansion | ||
| happens in a distinct ‘syntax context’, and each variable is tagged with the | ||
| syntax context where it was introduced. It’s as though the variable `state` | ||
| inside `main` is painted a different "color" from the variable `state` inside | ||
| inside `main` is painted a different‘color’from the variable `state` inside | ||
| the macro, and therefore they don’t conflict. | ||
|
|
||
| [hygienic macro system]: http://en.wikipedia.org/wiki/Hygienic_macro | ||
|
|
@@ -418,7 +418,7 @@ tell you about the syntax contexts. | |
| they are unstable and require feature gates. | ||
|
|
||
| * `log_syntax!(...)` will print its arguments to standard output, at compile | ||
| time, and "expand" to nothing. | ||
| time, and‘expand’to nothing. | ||
|
|
||
| * `trace_macros!(true)` will enable a compiler message every time a macro is | ||
| expanded. Use `trace_macros!(false)` later in expansion to turn it off. | ||
|
|
@@ -471,7 +471,7 @@ which syntactic form it matches. | |
| * `block`: a brace-delimited sequence of statements. Example: | ||
| `{ log(error, "hi"); return 12; }`. | ||
| * `item`: an [item][]. Examples: `fn foo() { }`; `struct Bar;`. | ||
| * `meta`: a "meta item", as found in attributes. Example: `cfg(target_os = "windows")`. | ||
| * `meta`: a‘meta item’, as found in attributes. Example: `cfg(target_os = "windows")`. | ||
| * `tt`: a single token tree. | ||
|
|
||
| There are additional rules regarding the next token after a metavariable: | ||
|
|
@@ -765,7 +765,7 @@ as `unimplemented!` until you’re ready to write them. | |
| # Procedural macros | ||
|
|
||
| If Rust’s macro system can’t do what you need, you may want to write a | ||
| [compiler plugin](plugins.html) instead. Compared to `macro_rules!` | ||
| [compiler plugin](compiler-plugins.html) instead. Compared to `macro_rules!` | ||
| macros, this is significantly more work, the interfaces are much less stable, | ||
| and bugs can be much harder to track down. In exchange you get the | ||
| flexibility of running arbitrary Rust code within the compiler. Syntax | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you delete the space between
aand'sum type'here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
en,they shouldn't be deleted as well as others, I will turn it back!