Skip to content

Commit 0f629e7

Browse files
committed
ch09 エラー処理の和訳を最新版に更新
rust-lang/book@19c40bf
1 parent d2d9bbb commit 0f629e7

File tree

55 files changed

+1138
-1040
lines changed

Some content is hidden

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

55 files changed

+1138
-1040
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "panic"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-01/output.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ $ cargo run
22
Compiling panic v0.1.0 (file:///projects/panic)
33
Finished dev [unoptimized + debuginfo] target(s) in 0.27s
44
Running `target/debug/panic`
5-
thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 99', /rustc/5e1a799842ba6ed4a57e91f7ab9435947482f7d8/src/libcore/slice/mod.rs:2806:10
6-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
5+
thread 'main' panicked at src/main.rs:4:6:
6+
index out of bounds: the len is 3 but the index is 99
7+
('main'スレッドはsrc/main.rs:4:6でパニックしました:
8+
境界外番号: 長さは3なのに、添え字は99です)
9+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fs::File;
22

33
fn main() {
4-
let f = File::open("hello.txt");
4+
let greeting_file_result = File::open("hello.txt");
55
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-04/output.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ $ cargo run
22
Compiling error-handling v0.1.0 (file:///projects/error-handling)
33
Finished dev [unoptimized + debuginfo] target(s) in 0.73s
44
Running `target/debug/error-handling`
5-
thread 'main' panicked at 'Problem opening the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:8:23
6-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
5+
thread 'main' panicked at src/main.rs:8:23:
6+
Problem opening the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }
7+
('main'スレッドは、src/main.rs:8:23でパニックしました:
8+
ファイルを開く際に問題がありました: Os { code: 2, kind: NotFound, message: "そのようなファイルやディレクトリはありません" })
9+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::fs::File;
22

33
fn main() {
4-
let f = File::open("hello.txt");
4+
let greeting_file_result = File::open("hello.txt");
55

6-
let f = match f {
6+
let greeting_file = match greeting_file_result {
77
Ok(file) => file,
8+
// "ファイルを開くのに問題がありました: {:?}"
89
Err(error) => panic!("Problem opening the file: {:?}", error),
910
};
1011
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-05/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ use std::fs::File;
22
use std::io::ErrorKind;
33

44
fn main() {
5-
let f = File::open("hello.txt");
5+
let greeting_file_result = File::open("hello.txt");
66

7-
let f = match f {
7+
let greeting_file = match greeting_file_result {
88
Ok(file) => file,
99
Err(error) => match error.kind() {
1010
ErrorKind::NotFound => match File::create("hello.txt") {
1111
Ok(fc) => fc,
12+
// "ファイルを作成するのに問題がありました: {:?}"
1213
Err(e) => panic!("Problem creating the file: {:?}", e),
1314
},
1415
other_error => {
15-
panic!("Problem opening the file: {:?}", other_error)
16+
// "ファイルを開くのに問題がありました: {:?}"
17+
panic!("Problem opening the file: {:?}", other_error);
1618
}
1719
},
1820
};
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-06/src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// ANCHOR: here
22
use std::fs::File;
3-
use std::io;
4-
use std::io::Read;
3+
use std::io::{self, Read};
54

65
fn read_username_from_file() -> Result<String, io::Error> {
7-
let f = File::open("hello.txt");
6+
let username_file_result = File::open("hello.txt");
87

9-
let mut f = match f {
8+
let mut username_file = match username_file_result {
109
Ok(file) => file,
1110
Err(e) => return Err(e),
1211
};
1312

14-
let mut s = String::new();
13+
let mut username = String::new();
1514

16-
match f.read_to_string(&mut s) {
17-
Ok(_) => Ok(s),
15+
match username_file.read_to_string(&mut username) {
16+
Ok(_) => Ok(username),
1817
Err(e) => Err(e),
1918
}
2019
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-07/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// ANCHOR: here
22
use std::fs::File;
3-
use std::io;
4-
use std::io::Read;
3+
use std::io::{self, Read};
54

65
fn read_username_from_file() -> Result<String, io::Error> {
7-
let mut f = File::open("hello.txt")?;
8-
let mut s = String::new();
9-
f.read_to_string(&mut s)?;
10-
Ok(s)
6+
let mut username_file = File::open("hello.txt")?;
7+
let mut username = String::new();
8+
username_file.read_to_string(&mut username)?;
9+
Ok(username)
1110
}
1211
// ANCHOR_END: here
1312

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-08/src/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// ANCHOR: here
22
use std::fs::File;
3-
use std::io;
4-
use std::io::Read;
3+
use std::io::{self, Read};
54

65
fn read_username_from_file() -> Result<String, io::Error> {
7-
let mut s = String::new();
6+
let mut username = String::new();
87

9-
File::open("hello.txt")?.read_to_string(&mut s)?;
8+
File::open("hello.txt")?.read_to_string(&mut username)?;
109

11-
Ok(s)
10+
Ok(username)
1211
}
1312
// ANCHOR_END: here
1413

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch09-error-handling/listing-09-10/Cargo.lock

Lines changed: 1 addition & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
[package]
2-
name = "guessing_game"
2+
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
8-
rand = "0.5.5"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
$ cargo run
2+
Compiling error-handling v0.1.0 (file:///projects/error-handling)
3+
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
4+
(エラー: `?`演算子は`Result`または`Option`(あるいはその他`FromResidual`を実装する型)を返す関数内でのみ使用できます
5+
--> src/main.rs:4:48
6+
|
7+
3 | fn main() {
8+
| --------- this function should return `Result` or `Option` to accept `?`
9+
(`?`を使えるようにするには、この関数は`Result`または`Option`を返すべきです)
10+
4 | let greeting_file = File::open("hello.txt")?;
11+
| ^ cannot use the `?` operator in a function that returns `()`
12+
(`()`を返す関数内では`?`演算子を使用できません)
13+
|
14+
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
15+
(ヘルプ: トレイト`FromResidual<Result<Infallible, std::io::Error>>`は`()`に対して実装されていません)
16+
17+
For more information about this error, try `rustc --explain E0277`.
18+
error: could not compile `error-handling` (bin "error-handling") due to 1 previous error
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
1-
use rand::Rng;
2-
use std::cmp::Ordering;
3-
use std::io;
4-
5-
// ANCHOR: here
6-
pub struct Guess {
7-
value: i32,
8-
}
9-
10-
impl Guess {
11-
pub fn new(value: i32) -> Guess {
12-
if value < 1 || value > 100 {
13-
panic!("Guess value must be between 1 and 100, got {}.", value);
14-
}
15-
16-
Guess { value }
17-
}
18-
19-
pub fn value(&self) -> i32 {
20-
self.value
21-
}
22-
}
23-
// ANCHOR_END: here
1+
use std::fs::File;
242

253
fn main() {
26-
println!("Guess the number!");
27-
28-
let secret_number = rand::thread_rng().gen_range(1, 101);
29-
30-
loop {
31-
println!("Please input your guess.");
32-
33-
let mut guess = String::new();
34-
35-
io::stdin()
36-
.read_line(&mut guess)
37-
.expect("Failed to read line");
38-
39-
let guess: i32 = match guess.trim().parse() {
40-
Ok(num) => num,
41-
Err(_) => continue,
42-
};
43-
44-
let guess = Guess::new(guess);
45-
46-
match guess.value().cmp(&secret_number) {
47-
Ordering::Less => println!("Too small!"),
48-
Ordering::Greater => println!("Too big!"),
49-
Ordering::Equal => {
50-
println!("You win!");
51-
break;
52-
}
53-
}
54-
}
4+
let greeting_file = File::open("hello.txt")?;
555
}

listings/ch09-error-handling/no-listing-07-main-returning-result/Cargo.lock renamed to listings/ch09-error-handling/listing-09-11/Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "error-handling"
33
version = "0.1.0"
4-
authors = ["Your Name <you@example.com>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

0 commit comments

Comments
 (0)