Skip to content

Commit fc8a1cf

Browse files
committed
Update original
1 parent 89d772f commit fc8a1cf

Some content is hidden

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

70 files changed

+1328
-353
lines changed

.examples-en

Submodule .examples-en updated 74 files

src/SUMMARY.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@
100100
- [match](flow_control/match.md)
101101
- [Destructuring](flow_control/match/destructuring.md)
102102
- [tuples](flow_control/match/destructuring/destructure_tuple.md)
103+
- [arrays/slices](flow_control/match/destructuring/destructure_slice.md)
103104
- [enums](flow_control/match/destructuring/destructure_enum.md)
104105
- [pointers/ref](flow_control/match/destructuring/destructure_pointers.md)
105106
- [structs](flow_control/match/destructuring/destructure_structures.md)
106107
- [Guards](flow_control/match/guard.md)
107108
- [Binding](flow_control/match/binding.md)
108109
- [if let](flow_control/if_let.md)
110+
- [let-else](flow_control/let_else.md)
109111
- [while let](flow_control/while_let.md)
110112
-->
111113
- [条件分岐](flow_control.md)
@@ -315,10 +317,12 @@
315317
<!--
316318
- [Error handling](error.md)
317319
- [`panic`](error/panic.md)
320+
- [`abort` & `unwind`](error/abort_unwind.md)
318321
- [`Option` & `unwrap`](error/option_unwrap.md)
319322
- [Unpacking options with `?`](error/option_unwrap/question_mark.md)
320323
- [Combinators: `map`](error/option_unwrap/map.md)
321324
- [Combinators: `and_then`](error/option_unwrap/and_then.md)
325+
- [Defaults: `or`, `or_else`, `get_or_insert`, 'get_or_insert_with`](error/option_unwrap/defaults.md)
322326
- [`Result`](error/result.md)
323327
- [`map` for `Result`](error/result/result_map.md)
324328
- [aliases for `Result`](error/result/result_alias.md)
@@ -423,6 +427,7 @@
423427

424428
<!--
425429
- [Unsafe Operations](unsafe.md)
430+
- [Inline assembly](unsafe/asm.md)
426431
-->
427432
- [安全でない操作](unsafe.md)
428433

@@ -432,7 +437,7 @@
432437
<!--
433438
- [Meta](meta.md)
434439
- [Documentation](meta/doc.md)
435-
- [Playpen](meta/playpen.md)
440+
- [Playground](meta/playground.md)
436441
-->
437442
- [周辺情報](meta.md)
438443
- [ドキュメンテーション](meta/doc.md)

src/attribute.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ can be used to/for:
1919
* link to a foreign library
2020
* mark functions as unit tests
2121
* mark functions that will be part of a benchmark
22+
* [attribute like macros][macros]
2223
-->
2324
* [コンパイル時の条件分岐][cfg]
2425
* [クレート名、バージョン、種類(バイナリか、ライブラリか)の設定][crate]
@@ -57,3 +58,4 @@ Attributes can have multiple values and can be separated over multiple lines, to
5758
[cfg]: attribute/cfg.md
5859
[crate]: attribute/crate.md
5960
[lint]: https://en.wikipedia.org/wiki/Lint_%28software%29
61+
[macros]: https://doc.rust-lang.org/book/ch19-06-macros.html#attribute-like-macros

src/attribute/cfg.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ utilize identical argument syntax.
2020
前者は条件付きコンパイルを行いますが、後者は`true`または`false`リテラルに評価され実行時にチェックすることが可能です。
2121
いずれの場合も適切なシンタックスで記述する必要があります。
2222

23+
`cfg!`, unlike `#[cfg]`, does not remove any code and only evaluates to true or false. For example, all blocks in an if/else expression need to be valid when `cfg!` is used for the condition, regardless of what `cfg!` is evaluating.
24+
2325
```rust,editable
2426
// This function only gets compiled if the target OS is linux
2527
// この関数はターゲットOSがLinuxの時のみコンパイルされる。

src/cargo/conventions.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ foo
3838
```
3939

4040
<!--
41-
To tell `cargo` to compile or run this binary as opposed to the default or other
42-
binaries, we just pass `cargo` the `--bin my_other_bin` flag, where `my_other_bin`
43-
is the name of the binary we want to work with.
41+
To tell `cargo` to only compile or run this binary, we just pass `cargo` the
42+
`--bin my_other_bin` flag, where `my_other_bin` is the name of the binary we
43+
want to work with.
4444
-->
45-
デフォルトバイナリや他のバイナリではなく、このバイナリをコンパイルや実行するように`cargo`に伝えるには、`cargo``--bin my_other_bin`フラグを渡します。ここでは`my_other_bin`が対象のバイナリの名前です。
45+
このバイナリだけをコンパイルや実行するように`cargo`に伝えるには、`cargo``--bin my_other_bin`フラグを渡します。ここでは`my_other_bin`が対象のバイナリの名前です。
4646

4747
<!--
4848
In addition to extra binaries, `cargo` supports [more features] such as

src/cargo/deps.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Rustのプロジェクトを新しく作るには下記のようにします。
2121
# バイナリ
2222
cargo new foo
2323

24-
# OR A library
25-
# またはライブラリ
26-
cargo new --lib foo
24+
# A library
25+
# ライブラリ
26+
cargo new --lib bar
2727
```
2828

2929
<!--
@@ -38,18 +38,23 @@ After the above commands, you should see a file hierarchy like this:
3838
上のコマンドを実行すると、次のようなファイル階層ができます。
3939

4040
```txt
41-
foo
42-
├── Cargo.toml
43-
└── src
44-
└── main.rs
41+
.
42+
├── bar
43+
│ ├── Cargo.toml
44+
│ └── src
45+
│ └── lib.rs
46+
└── foo
47+
├── Cargo.toml
48+
└── src
49+
└── main.rs
4550
```
4651

4752
<!--
48-
The `main.rs` is the root source file for your new project -- nothing new there.
49-
The `Cargo.toml` is the config file for `cargo` for this project (`foo`). If you
53+
The `main.rs` is the root source file for your new `foo` project -- nothing new there.
54+
The `Cargo.toml` is the config file for `cargo` for this project. If you
5055
look inside it, you should see something like this:
5156
-->
52-
`main.rs`がこの新規プロジェクトのルートのソースファイルです。なにも新しいことはありませんね。`Cargo.toml`はこのプロジェクト(`foo`)の`cargo`の設定ファイルです。中を見てみるとこのようになっています。
57+
`main.rs`がこの新規プロジェクト `foo` のルートのソースファイルです。なにも新しいことはありませんね。`Cargo.toml`はこのプロジェクトの`cargo`の設定ファイルです。中を見てみるとこのようになっています。
5358

5459
```toml
5560
[package]

src/conversion/from_into.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl From<i32> for Number {
8787
8888
fn main() {
8989
let int = 5;
90-
// Try removing the type declaration
90+
// Try removing the type annotation
9191
let num: Number = int.into();
9292
println!("My number is {:?}", num);
9393
}

src/crates/using_lib.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
```txt
2121
# Where library.rlib is the path to the compiled library, assumed that it's
2222
# in the same directory here:
23-
$ rustc executable.rs --extern rary=library.rlib --edition=2018 && ./executable
23+
$ rustc executable.rs --extern rary=library.rlib && ./executable
2424
called rary's `public_function()`
2525
called rary's `indirect_access()`, that
2626
> called rary's `private_function()`

src/custom_types/enum.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<!--
77
The `enum` keyword allows the creation of a type which may be one of a few
8-
different variants. Any variant which is valid as a `struct` is also valid as
8+
different variants. Any variant which is valid as a `struct` is also valid in
99
an `enum`.
1010
-->
1111
列挙型(`enum`)はいくつかの異なる要素型の中から1つを選ぶような場合に使用します。構造体(`struct`)の定義を満たすものならば何でも`enum` の要素型として使用できます。
@@ -21,7 +21,7 @@ an `enum`.
2121
// `KeyPress(char) != Paste(String)` である。
2222
// 要素型は互いに異なり、互いに非依存である。
2323
enum WebEvent {
24-
// An `enum` may either be `unit-like`,
24+
// An `enum` variant may either be `unit-like`,
2525
// `enum`要素型はユニット風でもよい
2626
PageLoad,
2727
PageUnload,
@@ -41,7 +41,7 @@ fn inspect(event: WebEvent) {
4141
match event {
4242
WebEvent::PageLoad => println!("page loaded"),
4343
WebEvent::PageUnload => println!("page unloaded"),
44-
// Destructure `c` from inside the `enum`.
44+
// Destructure `c` from inside the `enum` variant.
4545
WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
4646
WebEvent::Paste(s) => println!("pasted \"{}\".", s),
4747
// Destructure `Click` into `x` and `y`.

src/custom_types/structs.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ There are three types of structures ("structs") that can be created using the
1919
* ユニット。これはフィールドを持たず、ジェネリック型を扱う際に有効です。
2020

2121
```rust,editable
22+
// An attribute to hide warnings for unused code.
23+
#![allow(dead_code)]
24+
2225
#[derive(Debug)]
2326
struct Person {
2427
name: String,

src/error/abort_unwind.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# `abort` and `unwind`
2+
3+
The previous section illustrates the error handling mechanism `panic`. Different code paths can be conditionally compiled based on the panic setting. The current values available are `unwind` and `abort`.
4+
5+
6+
Building on the prior lemonade example, we explicitly use the panic strategy to exercise different lines of code.
7+
8+
```rust,editable,mdbook-runnable
9+
10+
fn drink(beverage: &str) {
11+
// You shouldn't drink too much sugary beverages.
12+
if beverage == "lemonade" {
13+
if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");}
14+
else{ println!("Spit it out!!!!");}
15+
}
16+
else{ println!("Some refreshing {} is all I need.", beverage); }
17+
}
18+
19+
fn main() {
20+
drink("water");
21+
drink("lemonade");
22+
}
23+
```
24+
25+
Here is another example focusing on rewriting `drink()` and explicitly use the `unwind` keyword.
26+
27+
```rust,editable
28+
29+
#[cfg(panic = "unwind")]
30+
fn ah(){ println!("Spit it out!!!!");}
31+
32+
#[cfg(not(panic="unwind"))]
33+
fn ah(){ println!("This is not your party. Run!!!!");}
34+
35+
fn drink(beverage: &str){
36+
if beverage == "lemonade"{ ah();}
37+
else{println!("Some refreshing {} is all I need.", beverage);}
38+
}
39+
40+
fn main() {
41+
drink("water");
42+
drink("lemonade");
43+
}
44+
```
45+
46+
The panic strategy can be set from the command line by using `abort` or `unwind`.
47+
48+
```console
49+
rustc lemonade.rs -C panic=abort
50+
```
51+

src/error/iter_result.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,36 @@ fn main() {
4545
}
4646
```
4747

48+
## Collect the failed items with `map_err()` and `filter_map()`
49+
50+
`map_err` calls a function with the error, so by adding that to the previous
51+
`filter_map` solution we can save them off to the side while iterating.
52+
53+
```rust,editable
54+
fn main() {
55+
let strings = vec!["42", "tofu", "93", "999", "18"];
56+
let mut errors = vec![];
57+
let numbers: Vec<_> = strings
58+
.into_iter()
59+
.map(|s| s.parse::<u8>())
60+
.filter_map(|r| r.map_err(|e| errors.push(e)).ok())
61+
.collect();
62+
println!("Numbers: {:?}", numbers);
63+
println!("Errors: {:?}", errors);
64+
}
65+
```
66+
4867
<!--
4968
## Fail the entire operation with `collect()`
5069
-->
5170
## `collect()`で処理全体を失敗させる
5271

5372
<!--
54-
`Result` implements `FromIter` so that a vector of results (`Vec<Result<T, E>>`)
73+
`Result` implements `FromIterator` so that a vector of results (`Vec<Result<T, E>>`)
5574
can be turned into a result with a vector (`Result<Vec<T>, E>`). Once an
5675
`Result::Err` is found, the iteration will terminate.
5776
-->
58-
`Result`は、それらのベクトル(`Vec<Result<T, E>>`)からベクトルのそれ(`Result<Vec<T>, E>`)へと変換できるようにするため、`FromIter`を実装します。`Result::Err`が見つかり次第、イテレーションは終了します。
77+
`Result`は、それらのベクトル(`Vec<Result<T, E>>`)からベクトルのそれ(`Result<Vec<T>, E>`)へと変換できるようにするため、`FromIterator`を実装します。`Result::Err`が見つかり次第、イテレーションは終了します。
5978

6079
```rust,editable
6180
fn main() {

src/error/multiple_error_types/wrap_error.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Boxする方法の代替として、エラーを自前のエラー型として
1010

1111
```rust,editable
1212
use std::error;
13-
use std::error::Error as _;
13+
use std::error::Error;
1414
use std::num::ParseIntError;
1515
use std::fmt;
1616

0 commit comments

Comments
 (0)