Browse Source

update original

pull/1344/merge
funkill2 5 months ago
parent
commit
dc2119b78d
  1. 1
      rustbook-en/ci/dictionary.txt
  2. 72
      rustbook-en/nostarch/appendix.md
  3. 26
      rustbook-en/nostarch/appendix_b.md
  4. 43
      rustbook-en/nostarch/appendix_d.md
  5. BIN
      rustbook-en/nostarch/docx/appendix_b.docx
  6. BIN
      rustbook-en/nostarch/docx/appendix_d.docx
  7. 32
      rustbook-en/src/appendix-02-operators.md
  8. 40
      rustbook-en/src/appendix-04-useful-development-tools.md

1
rustbook-en/ci/dictionary.txt

@ -310,6 +310,7 @@ memoization @@ -310,6 +310,7 @@ memoization
metadata
Metadata
metaprogramming
metavariable
mibbit
Mibbit
microcontroller

72
rustbook-en/nostarch/appendix.md

@ -183,8 +183,10 @@ Table B-1: Operators @@ -183,8 +183,10 @@ Table B-1: Operators
|`-`|`- expr`|Arithmetic negation|`Neg`|
|`-`|`expr - expr`|Arithmetic subtraction|`Sub`|
|`-=`|`var -= expr`|Arithmetic subtraction and assignment|`SubAssign`|
|`->`|`fn(...) -> type`, <code>\|…\| -> type</code>|Function and closure return type||
|`.`|`expr.ident`|Member access||
|`->`|`fn(...) -> type`, <code>\|...\| -> type</code>|Function and closure return type||
|`.`|`expr.ident`|Field access||
|`.`|`expr.ident(expr, ...)`|Method call||
|`.`|`expr.0`, `expr.1`, etc.|Tuple indexing||
|`..`|`..`, `expr..`, `..expr`, `expr..expr`|Right-exclusive range literal|`PartialOrd`|
|`..=`|`..=expr`, `expr..=expr`|Right-inclusive range literal|`PartialOrd`|
|`..`|`..expr`|Struct literal update syntax||
@ -230,14 +232,14 @@ Table B-2: Stand-Alone Syntax @@ -230,14 +232,14 @@ Table B-2: Stand-Alone Syntax
|Symbol|Explanation|
|------|-----------|
|`'ident`|Named lifetime or loop label|
|`...u8`, `...i32`, `...f64`, `...usize`, etc.|Numeric literal of specific type|
|Digits immediately followed by `u8`, `i32`, `f64`, `usize`, and so on|Numeric literal of specific type|
|`"..."`|String literal|
|`r"..."`, `r#"..."#`, `r##"..."##`, etc.|Raw string literal, escape characters not processed|
|`b"..."`|Byte string literal; constructs an array of bytes instead of a string|
|`br"..."`, `br#"..."#`, `br##"..."##`, etc.|Raw byte string literal, combination of raw and byte string literal|
|`'...'`|Character literal|
|`b'...'`|ASCII byte literal|
|<code>\|\| expr</code>|Closure|
|<code>\|...\| expr</code>|Closure|
|`!`|Always empty bottom type for diverging functions|
|`_`|“Ignored” pattern binding; also used to make integer literals readable|
@ -298,8 +300,8 @@ Table B-6: Macros and Attributes @@ -298,8 +300,8 @@ Table B-6: Macros and Attributes
|`#[meta]`|Outer attribute|
|`#![meta]`|Inner attribute|
|`$ident`|Macro substitution|
|`$ident:kind`|Macro capture|
|`$(…)…`|Macro repetition|
|`$ident:kind`|Macro metavariable|
|`$(...)...`|Macro repetition|
|`ident!(...)`, `ident!{...}`, `ident![...]`|Macro invocation|
Table B-7 shows symbols that create comments.
@ -315,9 +317,9 @@ Table B-7: Comments @@ -315,9 +317,9 @@ Table B-7: 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 shows the contexts in which parentheses are used.
Table B-8: Tuples
Table B-8: Parentheses
|Symbol|Explanation|
|------|-----------|
@ -328,7 +330,6 @@ Table B-8: Tuples @@ -328,7 +330,6 @@ Table B-8: Tuples
|`(expr, ...)`|Tuple expression|
|`(type, ...)`|Tuple type|
|`expr(expr, ...)`|Function call expression; also used to initialize tuple `struct`s and tuple `enum` variants|
|`expr.0`, `expr.1`, etc.|Tuple indexing|
Table B-9 shows the contexts in which curly braces are used.
@ -337,7 +338,7 @@ Table B-9: Curly Brackets @@ -337,7 +338,7 @@ Table B-9: Curly Brackets
|Context|Explanation|
|-------|-----------|
|`{...}`|Block expression|
|`Type {...}`|`struct` literal|
|`Type {...}`|Struct literal|
Table B-10 shows the contexts in which square brackets are used.
@ -542,14 +543,11 @@ The `rustfmt` tool reformats your code according to the community code style. @@ -542,14 +543,11 @@ The `rustfmt` tool reformats your code according to the community code style.
Many collaborative projects use `rustfmt` to prevent arguments about which
style to use when writing Rust: everyone formats their code using the tool.
To install `rustfmt`, enter the following:
```
$ rustup component add rustfmt
```
This command gives you `rustfmt` and `cargo-fmt`, similar to how Rust gives you
both `rustc` and `cargo`. To format any Cargo project, enter the following:
Rust installations include `rustfmt` by default, so you should already have the
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
any Cargo project, enter the following:
```
$ cargo fmt
@ -561,9 +559,9 @@ on `rustfmt`, see its documentation at *https://github.com/rust-lang/rustfmt*. @@ -561,9 +559,9 @@ on `rustfmt`, see its documentation at *https://github.com/rust-lang/rustfmt*.
### Fix Your Code with rustfix
The rustfix tool is included with Rust installations and can automatically fix
The `rustfix` tool is included with Rust installations and can automatically fix
compiler warnings that have a clear way to correct the problem that’s likely
what you want. It’s likely you’ve seen compiler warnings before. For example,
what you want. You’ve probably seen compiler warnings before. For example,
consider this code:
Filename: src/main.rs
@ -575,8 +573,8 @@ fn main() { @@ -575,8 +573,8 @@ fn main() {
}
```
Here, we’re defining variable `x` as mutable, but we never actually mutate it.
Rust warns us about that:
Here, we’re defining the variable `x` as mutable, but we never actually mutate
it. Rust warns us about that:
```
$ cargo build
@ -614,7 +612,7 @@ fn main() { @@ -614,7 +612,7 @@ fn main() {
}
```
The `x` variable is now immutable, and the warning no longer appears.
The variable `x` is now immutable, and the warning no longer appears.
You can also use the `cargo fix` command to transition your code between
different Rust editions. Editions are covered in Appendix E at *appendix-05-editions.md*.
@ -622,13 +620,8 @@ different Rust editions. Editions are covered in Appendix E at *appendix-05-edit @@ -622,13 +620,8 @@ different Rust editions. Editions are covered in Appendix E at *appendix-05-edit
### More Lints with Clippy
The Clippy tool is a collection of lints to analyze your code so you can catch
common mistakes and improve your Rust code.
To install Clippy, enter the following:
```
$ rustup component add clippy
```
common mistakes and improve your Rust code. Clippy is included with standard
Rust installations.
To run Clippy’s lints on any Cargo project, enter the following:
@ -639,7 +632,7 @@ $ cargo clippy @@ -639,7 +632,7 @@ $ cargo clippy
For example, say you write a program that uses an approximation of a
mathematical constant, such as pi, as this program does:
Filename: src/main.rs
src/main.rs
```
fn main() {
@ -649,6 +642,8 @@ fn main() { @@ -649,6 +642,8 @@ fn main() {
}
```
Running `cargo clippy` on this project results in this error:
```
@ -665,10 +660,11 @@ error: approximate value of `f{32, 64}::consts::PI` found @@ -665,10 +660,11 @@ error: approximate value of `f{32, 64}::consts::PI` found
This error lets you know that Rust already has a more precise `PI` constant
defined, and that your program would be more correct if you used the constant
instead. You would then change your code to use the `PI` constant. The
following code doesn’t result in any errors or warnings from Clippy:
instead. You would then change your code to use the `PI` constant.
Filename: src/main.rs
The following code doesn’t result in any errors or warnings from Clippy:
src/main.rs
```
fn main() {
@ -678,19 +674,21 @@ fn main() { @@ -678,19 +674,21 @@ fn main() {
}
```
For more information on Clippy, see its documentation at *https://github.com/rust-lang/rust-clippy*.
### IDE Integration Using rust-analyzer
To help IDE integration, the Rust community recommends using
To help with IDE integration, the Rust community recommends using
`rust-analyzer`. This tool is a set of
compiler-centric utilities that speaks the Language Server Protocol, which is a specification for IDEs and programming languages to
compiler-centric utilities that speak Language Server Protocol, which is a specification for IDEs and programming languages to
communicate with each other. Different clients can use `rust-analyzer`, such as
the Rust analyzer plug-in for Visual Studio Code at *https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer*.
Visit the `rust-analyzer` project’s home page
for installation instructions, then install the language server support in your
particular IDE. Your IDE will gain abilities such as autocompletion, jump to
particular IDE. Your IDE will gain capabilities such as autocompletion, jump to
definition, and inline errors.
## Appendix E - Editions

26
rustbook-en/nostarch/appendix_b.md

@ -46,9 +46,10 @@ type | | @@ -46,9 +46,10 @@ type | |
| `-` | `- expr` | Arithmetic negation | `Neg` |
| `-` | `expr - expr` | Arithmetic subtraction | `Sub` |
| `-=` | `var -= expr` | Arithmetic subtraction and assignment | `SubAssign` |
| `->` | `fn(...) -> type`, `|…| -> type` | Function and closure return type |
|
| `. | `expr.ident` | Member access | |
| `->` | `fn(...) -> type`, `|...| -> type` | Function and closure return type | |
| `.` | `expr.ident` | Field access | |
| `.` | `expr.ident(expr, ...)` | Method call | |
| `.` | `expr.0`, `expr.1`, etc. | Tuple indexing | |
| `..` | `..`, `expr..`, `..expr`, `expr..expr` | Right-exclusive range literal
| `PartialOrd` |
| `..=` | `..=expr`, `expr..=expr` | Right-inclusive range literal |
@ -98,8 +99,8 @@ Table B-2: Stand-Alone Syntax @@ -98,8 +99,8 @@ Table B-2: Stand-Alone Syntax
| Symbol | Explanation |
|---|---|
| `'ident` | Named lifetime or loop label |
| `...u8`, `...i32`, `...f64`, `...usize`, and so on | Numeric literal of
specific type |
| Digits immediately followed by `u8`, `i32`, `f64`, `usize`, and so on |
Numeric literal of specific type |
| `"..."` | String literal |
| `r"..."`, `r#"..."#`, `r##"..."##`, and so on | Raw string literal; escape
characters not processed |
@ -109,7 +110,7 @@ string | @@ -109,7 +110,7 @@ string |
combination of raw and byte string literal |
| `'...'` | Character literal |
| `b'...'` | ASCII byte literal |
| `|| expr` | Closure |
| `|...| expr` | Closure |
| `!` | Always-empty bottom type for diverging functions |
| `_` | “Ignored” pattern binding; also used to make integer literals readable |
@ -164,7 +165,7 @@ Table B-5: Trait Bound Constraints @@ -164,7 +165,7 @@ Table B-5: Trait Bound Constraints
| Symbol | Explanation |
|---|---|
| T: U` | Generic parameter `T` constrained to types that implement `U` |
| `T: U` | Generic parameter `T` constrained to types that implement `U` |
| `T: 'a` | Generic type `T` must outlive lifetime `'a` (meaning the type
cannot transitively contain any references with lifetimes shorter than `'a`) |
| `T: 'static` | Generic type `T` contains no borrowed references other than
@ -183,8 +184,8 @@ Table B-6: Macros and Attributes @@ -183,8 +184,8 @@ Table B-6: Macros and Attributes
| `#[meta]` | Outer attribute |
| `#![meta]` | Inner attribute |
| `$ident` | Macro substitution |
| `$ident:kind` | Macro capture |
| `$(…)…` | Macro repetition |
| `$ident:kind` | Macro metavariable |
| `$(...)...` | Macro repetition |
| `ident!(...)`, `ident!{...}`, `ident![...]` | Macro invocation |
Table B-7 shows symbols that create comments.
@ -200,9 +201,9 @@ Table B-7: Comments @@ -200,9 +201,9 @@ Table B-7: 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 shows the contexts in which parentheses are used.
Table B-8: Tuples
Table B-8: Parentheses
| Symbol | Explanation |
|---|---|
@ -214,7 +215,6 @@ Table B-8: Tuples @@ -214,7 +215,6 @@ Table B-8: Tuples
| `(type, ...)` | Tuple type |
| `expr(expr, ...)` | Function call expression; also used to initialize tuple
`struct`s and tuple `enum` variants |
| `expr.0`, `expr.1`, and so on | Tuple indexing |
Table B-9 shows the contexts in which curly brackets are used.
@ -223,7 +223,7 @@ Table B-9: Curly Brackets @@ -223,7 +223,7 @@ Table B-9: Curly Brackets
| Context | Explanation |
|---|---|
| `{...}` | Block expression |
| `Type {...}` | `struct` literal |
| `Type {...}` | Struct literal |
Table B-10 shows the contexts in which square brackets are used.

43
rustbook-en/nostarch/appendix_d.md

@ -20,7 +20,7 @@ style to use when writing Rust: everyone formats their code using the tool. @@ -20,7 +20,7 @@ style to use when writing Rust: everyone formats their code using the tool.
Rust installations include `rustfmt` by default, so you should already have the
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
analagous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
any Cargo project, enter the following:
@ -42,34 +42,30 @@ example, consider this code: @@ -42,34 +42,30 @@ example, consider this code:
Filename: src/main.rs
```
fn do_something() {}
fn main() {
for i in 0..100 {
do_something();
}
let mut x = 42;
println!("{x}");
}
```
Here, we’re calling the `do_something` function 100 times, but we never use the
variable `i` in the body of the `for` loop. Rust warns us about that:
Here, we’re defining the variable `x` as mutable, but we never actually mutate
it. Rust warns us about that:
```
$ cargo build
Compiling myprogram v0.1.0 (file:///projects/myprogram)
warning: unused variable: `i`
--> src/main.rs:4:9
warning: variable does not need to be mutable
--> src/main.rs:2:9
|
4 | for i in 0..100 {
| ^ help: consider using `_i` instead
2 | let mut x = 0;
| ----^
| |
| help: remove this `mut`
|
= note: #[warn(unused_variables)] on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
= note: `#[warn(unused_mut)]` on by default
```
The warning suggests that we use `_i` as a name instead: the underscore
indicates that we intend for this variable to be unused. We can automatically
The warning suggests that we remove the `mut` keyword. We can automatically
apply that suggestion using the `rustfix` tool by running the command `cargo
fix`:
@ -86,16 +82,13 @@ code: @@ -86,16 +82,13 @@ code:
Filename: src/main.rs
```
fn do_something() {}
fn main() {
for _i in 0..100 {
do_something();
}
let x = 42;
println!("{x}");
}
```
The `for` loop variable is now named `_i`, and the warning no longer appears.
The variable `x` is now immutable, and the warning no longer appears.
You can also use the `cargo fix` command to transition your code between
different Rust editions. Editions are covered in Appendix E.
@ -157,7 +150,7 @@ fn main() { @@ -157,7 +150,7 @@ fn main() {
```
For more information on Clippy, see its documentation at
*https://github.com/rust-lang/rust-clippy**.*
*https://github.com/rust-lang/rust-clippy*.
## IDE Integration Using rust-analyzer
@ -171,5 +164,5 @@ languages to communicate with each other. Different clients can use @@ -171,5 +164,5 @@ languages to communicate with each other. Different clients can use
Visit the `rust-analyzer` project’s home page at
*https://rust-analyzer.github.io* for installation instructions, then install
the language server support in your particular IDE. Your IDE will gain
capabilities such as autocompletion, jump to definition, and inline errors
capabilities such as autocompletion, jump to definition, and inline errors.

BIN
rustbook-en/nostarch/docx/appendix_b.docx

Binary file not shown.

BIN
rustbook-en/nostarch/docx/appendix_d.docx

Binary file not shown.

32
rustbook-en/src/appendix-02-operators.md

@ -82,19 +82,19 @@ locations. @@ -82,19 +82,19 @@ locations.
<span class="caption">Table B-2: Stand-Alone Syntax</span>
| Symbol | Explanation |
| --------------------------------------------- | ---------------------------------------------------------------------- |
| `'ident` | Named lifetime or loop label |
| `...u8`, `...i32`, `...f64`, `...usize`, etc. | Numeric literal of specific type |
| `"..."` | String literal |
| `r"..."`, `r#"..."#`, `r##"..."##`, etc. | Raw string literal, escape characters not processed |
| `b"..."` | Byte string literal; constructs an array of bytes instead of a string |
| `br"..."`, `br#"..."#`, `br##"..."##`, etc. | Raw byte string literal, combination of raw and byte string literal |
| `'...'` | Character literal |
| `b'...'` | ASCII byte literal |
| <code>&vert;...&vert; expr</code> | Closure |
| `!` | Always empty bottom type for diverging functions |
| `_` | “Ignored” pattern binding; also used to make integer literals readable |
| Symbol | Explanation |
| ---------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `'ident` | Named lifetime or loop label |
| Digits immediately followed by `u8`, `i32`, `f64`, `usize`, and so on | Numeric literal of specific type |
| `"..."` | String literal |
| `r"..."`, `r#"..."#`, `r##"..."##`, etc. | Raw string literal, escape characters not processed |
| `b"..."` | Byte string literal; constructs an array of bytes instead of a string |
| `br"..."`, `br#"..."#`, `br##"..."##`, etc. | Raw byte string literal, combination of raw and byte string literal |
| `'...'` | Character literal |
| `b'...'` | ASCII byte literal |
| <code>&vert;...&vert; expr</code> | Closure |
| `!` | 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
hierarchy to an item.
@ -153,8 +153,8 @@ macros and specifying attributes on an item. @@ -153,8 +153,8 @@ macros and specifying attributes on an item.
| `#[meta]` | Outer attribute |
| `#![meta]` | Inner attribute |
| `$ident` | Macro substitution |
| `$ident:kind` | Macro capture |
| `$(…)…` | Macro repetition |
| `$ident:kind` | Macro metavariable |
| `$(...)...` | Macro repetition |
| `ident!(...)`, `ident!{...}`, `ident![...]` | Macro invocation |
Table B-7 shows symbols that create comments.
@ -191,7 +191,7 @@ Table B-9 shows the contexts in which curly braces are used. @@ -191,7 +191,7 @@ Table B-9 shows the contexts in which curly braces are used.
| Context | Explanation |
| ------------ | ---------------- |
| `{...}` | Block expression |
| `Type {...}` | `struct` literal |
| `Type {...}` | Struct literal |
Table B-10 shows the contexts in which square brackets are used.

40
rustbook-en/src/appendix-04-useful-development-tools.md

@ -10,22 +10,12 @@ The `rustfmt` tool reformats your code according to the community code style. @@ -10,22 +10,12 @@ The `rustfmt` tool reformats your code according to the community code style.
Many collaborative projects use `rustfmt` to prevent arguments about which
style to use when writing Rust: everyone formats their code using the tool.
Rust installations include rustfmt by default, so you should already have the
Rust installations include `rustfmt` by default, so you should already have the
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
any Cargo project, enter the following:
```sh
$ cargo fmt
```
Running this command reformats all the Rust code in the current crate. This
should only change the code style, not the code semantics.
This command gives you `rustfmt` and `cargo-fmt`, similar to how Rust gives you
both `rustc` and `cargo`. To format any Cargo project, enter the following:
```console
$ cargo fmt
```
@ -34,13 +24,11 @@ Running this command reformats all the Rust code in the current crate. This @@ -34,13 +24,11 @@ Running this command reformats all the Rust code in the current crate. This
should only change the code style, not the code semantics. For more information
on `rustfmt`, see [its documentation][rustfmt].
[rustfmt]: https://github.com/rust-lang/rustfmt
### Fix Your Code with `rustfix`
The `rustfix` tool is included with Rust installations and can automatically fix
compiler warnings that have a clear way to correct the problem that’s likely
what you want. It’s likely you’ve seen compiler warnings before. For example,
what you want. You’ve probably seen compiler warnings before. For example,
consider this code:
<span class="filename">Filename: src/main.rs</span>
@ -92,7 +80,7 @@ fn main() { @@ -92,7 +80,7 @@ fn main() {
}
```
The `x` variable is now immutable, and the warning no longer appears.
The variable `x` is now immutable, and the warning no longer appears.
You can also use the `cargo fix` command to transition your code between
different Rust editions. Editions are covered in [Appendix E][editions].
@ -140,8 +128,9 @@ error: approximate value of `f{32, 64}::consts::PI` found @@ -140,8 +128,9 @@ error: approximate value of `f{32, 64}::consts::PI` found
This error lets you know that Rust already has a more precise `PI` constant
defined, and that your program would be more correct if you used the constant
instead. You would then change your code to use the `PI` constant. The
following code doesn’t result in any errors or warnings from Clippy:
instead. You would then change your code to use the `PI` constant.
The following code doesn’t result in any errors or warnings from Clippy:
<Listing file-name="src/main.rs">
@ -157,24 +146,23 @@ fn main() { @@ -157,24 +146,23 @@ fn main() {
For more information on Clippy, see [its documentation][clippy].
[clippy]: https://github.com/rust-lang/rust-clippy
### IDE Integration Using `rust-analyzer`
To help IDE integration, the Rust community recommends using
To help with IDE integration, the Rust community recommends using
[`rust-analyzer`][rust-analyzer]<!-- ignore -->. This tool is a set of
compiler-centric utilities that speaks the [Language Server Protocol][lsp]<!--
compiler-centric utilities that speak [Language Server Protocol][lsp]<!--
ignore -->, which is a specification for IDEs and programming languages to
communicate with each other. Different clients can use `rust-analyzer`, such as
[the Rust analyzer plug-in for Visual Studio Code][vscode].
[lsp]: http://langserver.org/
[vscode]: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer
Visit the `rust-analyzer` project’s [home page][rust-analyzer]<!-- ignore -->
for installation instructions, then install the language server support in your
particular IDE. Your IDE will gain abilities such as autocompletion, jump to
particular IDE. Your IDE will gain capabilities such as autocompletion, jump to
definition, and inline errors.
[rust-analyzer]: https://rust-analyzer.github.io
[rustfmt]: https://github.com/rust-lang/rustfmt
[editions]: appendix-05-editions.md
[clippy]: https://github.com/rust-lang/rust-clippy
[rust-analyzer]: https://rust-analyzer.github.io
[lsp]: http://langserver.org/
[vscode]: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer

Loading…
Cancel
Save