Skip to content

Commit b7f9baf

Browse files
committed
ch16 恐れるな!並行性の和訳を最新版に更新
rust-lang/book@19c40bf
1 parent 97d8397 commit b7f9baf

File tree

35 files changed

+440
-875
lines changed

35 files changed

+440
-875
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch16-fearless-concurrency/listing-16-01/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ use std::time::Duration;
44
fn main() {
55
thread::spawn(|| {
66
for i in 1..10 {
7+
// "やあ!立ち上げたスレッドから数字{}だよ!"
78
println!("hi number {} from the spawned thread!", i);
89
thread::sleep(Duration::from_millis(1));
910
}
1011
});
1112

1213
for i in 1..5 {
14+
// "やあ!メインスレッドから数字{}だよ!"
1315
println!("hi number {} from the main thread!", i);
1416
thread::sleep(Duration::from_millis(1));
1517
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
$ cargo run
22
Compiling threads v0.1.0 (file:///projects/threads)
33
error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function
4+
(エラー: クロージャは現在の関数よりも長生きするかもしれませんが、現在の関数が所有している`v`を借用しています)
45
--> src/main.rs:6:32
56
|
67
6 | let handle = thread::spawn(|| {
78
| ^^ may outlive borrowed value `v`
9+
| (借用されている値`v`より長生きするかもしれません)
810
7 | println!("Here's a vector: {:?}", v);
911
| - `v` is borrowed here
12+
| (`v`はここで借用されています)
1013
|
1114
note: function requires argument type to outlive `'static`
15+
(注釈: 関数は引数型が`'static`より長生きすることを要求しています)
1216
--> src/main.rs:6:18
1317
|
1418
6 | let handle = thread::spawn(|| {
@@ -17,13 +21,10 @@ note: function requires argument type to outlive `'static`
1721
8 | | });
1822
| |______^
1923
help: to force the closure to take ownership of `v` (and any other referenced variables), use the `move` keyword
24+
(ヘルプ: `v`(や他の参照されている変数)の所有権をクロージャに奪わせるには、`move`キーワードを使用してください)
2025
|
2126
6 | let handle = thread::spawn(move || {
22-
| ^^^^^^^
23-
24-
error: aborting due to previous error
27+
| ++++
2528

2629
For more information about this error, try `rustc --explain E0373`.
27-
error: could not compile `threads`.
28-
29-
To learn more, run the command again with --verbose.
30+
error: could not compile `threads` (bin "threads") due to 1 previous error

listings/ch16-fearless-concurrency/listing-16-03/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ fn main() {
44
let v = vec![1, 2, 3];
55

66
let handle = thread::spawn(|| {
7+
// "こちらがベクタ: {:?}"
78
println!("Here's a vector: {:?}", v);
89
});
910

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch16-fearless-concurrency/listing-16-04/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
println!("Here's a vector: {:?}", v);
88
});
99

10-
drop(v); // oh no!
10+
drop(v); // あちゃー!
1111

1212
handle.join().unwrap();
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "message-passing"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "message-passing"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "message-passing"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch16-fearless-concurrency/listing-16-08/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ fn main() {
1010
});
1111

1212
let received = rx.recv().unwrap();
13+
// "取得しました: {}"
1314
println!("Got: {}", received);
1415
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "message-passing"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
$ cargo run
22
Compiling message-passing v0.1.0 (file:///projects/message-passing)
33
error[E0382]: borrow of moved value: `val`
4+
(エラー: ムーブされた値の借用: `val`)
45
--> src/main.rs:10:31
56
|
67
8 | let val = String::from("hi");
7-
| --- move occurs because `val` has type `std::string::String`, which does not implement the `Copy` trait
8+
| --- move occurs because `val` has type `String`, which does not implement the `Copy` trait
9+
| (`val`は`Copy`トレイトを実装しない`String`型を持つので、ムーブが発生します)
810
9 | tx.send(val).unwrap();
911
| --- value moved here
12+
| (値はここでムーブされます)
1013
10 | println!("val is {}", val);
1114
| ^^^ value borrowed here after move
12-
13-
error: aborting due to previous error
15+
| (ここでムーブ後に借用されます)
16+
|
17+
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
18+
help: consider cloning the value if the performance cost is acceptable
19+
(ヘルプ: パフォーマンスコストが許容できる場合は、クローンすることを検討してください)
20+
|
21+
9 | tx.send(val.clone()).unwrap();
22+
| ++++++++
1423

1524
For more information about this error, try `rustc --explain E0382`.
16-
error: could not compile `message-passing`.
17-
18-
To learn more, run the command again with --verbose.
25+
error: could not compile `message-passing` (bin "message-passing") due to 1 previous error

listings/ch16-fearless-concurrency/listing-16-09/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn main() {
77
thread::spawn(move || {
88
let val = String::from("hi");
99
tx.send(val).unwrap();
10+
// "valは{}です"
1011
println!("val is {}", val);
1112
});
1213

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "message-passing"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch16-fearless-concurrency/listing-16-10/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() {
66
let (tx, rx) = mpsc::channel();
77

88
thread::spawn(move || {
9+
// スレッドからやあ (hi from the thread)
910
let vals = vec![
1011
String::from("hi"),
1112
String::from("from"),
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "message-passing"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch16-fearless-concurrency/listing-16-11/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88

99
let (tx, rx) = mpsc::channel();
1010

11-
let tx1 = mpsc::Sender::clone(&tx);
11+
let tx1 = tx.clone();
1212
thread::spawn(move || {
1313
let vals = vec![
1414
String::from("hi"),
@@ -24,6 +24,7 @@ fn main() {
2424
});
2525

2626
thread::spawn(move || {
27+
// 君のためにもっとメッセージを (more messages for you)
2728
let vals = vec![
2829
String::from("more"),
2930
String::from("messages"),
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "shared-state"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "shared-state"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
$ cargo run
22
Compiling shared-state v0.1.0 (file:///projects/shared-state)
3-
error[E0382]: use of moved value: `counter`
4-
--> src/main.rs:9:36
3+
error[E0382]: borrow of moved value: `counter`
4+
(エラー: ムーブされた値の借用: `counter`)
5+
--> src/main.rs:21:29
56
|
67
5 | let counter = Mutex::new(0);
7-
| ------- move occurs because `counter` has type `std::sync::Mutex<i32>`, which does not implement the `Copy` trait
8+
| ------- move occurs because `counter` has type `Mutex<i32>`, which does not implement the `Copy` trait
9+
| (`counter`は`Copy`トレイトを実装しない`Mutex<i32>`型を持つので、ムーブが発生します)
810
...
911
9 | let handle = thread::spawn(move || {
10-
| ^^^^^^^ value moved into closure here, in previous iteration of loop
11-
10 | let mut num = counter.lock().unwrap();
12-
| ------- use occurs due to use in closure
13-
14-
error: aborting due to previous error
12+
| ------- value moved into closure here, in previous iteration of loop
13+
| (値は、ループの前回の反復時に、ここでクロージャ内にムーブされます)
14+
...
15+
21 | println!("Result: {}", *counter.lock().unwrap());
16+
| ^^^^^^^ value borrowed here after move
17+
| (値はここでムーブ後に借用されています)
1518

1619
For more information about this error, try `rustc --explain E0382`.
17-
error: could not compile `shared-state`.
18-
19-
To learn more, run the command again with --verbose.
20+
error: could not compile `shared-state` (bin "shared-state") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "shared-state"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
$ cargo run
22
Compiling shared-state v0.1.0 (file:///projects/shared-state)
3-
error[E0277]: `std::rc::Rc<std::sync::Mutex<i32>>` cannot be sent between threads safely
4-
--> src/main.rs:11:22
5-
|
6-
11 | let handle = thread::spawn(move || {
7-
| ^^^^^^^^^^^^^ `std::rc::Rc<std::sync::Mutex<i32>>` cannot be sent between threads safely
8-
|
9-
= help: within `[closure@src/main.rs:11:36: 15:10 counter:std::rc::Rc<std::sync::Mutex<i32>>]`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::sync::Mutex<i32>>`
10-
= note: required because it appears within the type `[closure@src/main.rs:11:36: 15:10 counter:std::rc::Rc<std::sync::Mutex<i32>>]`
11-
12-
error: aborting due to previous error
3+
error[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely
4+
(エラー: `Rc<Mutex<i32>>`はスレッド間で安全に送信できません)
5+
--> src/main.rs:11:36
6+
|
7+
11 | let handle = thread::spawn(move || {
8+
| ------------- ^------
9+
| | |
10+
| ______________________|_____________within this `{closure@src/main.rs:11:36: 11:43}`
11+
| | | (この`{[closure@src/main.rs:11:36: 11:43}`の中で)
12+
| | |
13+
| | required by a bound introduced by this call
14+
| | (この呼び出しによって導入される境界によって必要とされます)
15+
12 | | let mut num = counter.lock().unwrap();
16+
13 | |
17+
14 | | *num += 1;
18+
15 | | });
19+
| |_________^ `Rc<Mutex<i32>>` cannot be sent between threads safely
20+
| (`Rc<Mutex<i32>>`はスレッド間で安全に送信できません)
21+
|
22+
= help: within `{closure@src/main.rs:11:36: 11:43}`, the trait `Send` is not implemented for `Rc<Mutex<i32>>`
23+
=(ヘルプ: `{closure@src/main.rs:11:36: 11:43}`の中で、トレイト`Send`は`Rc<Mutex<i32>>`に対して実装されていません
24+
note: required because it's used within this closure
25+
(注釈: このクロージャの中で使用されているので、要求されます)
26+
--> src/main.rs:11:36
27+
|
28+
11 | let handle = thread::spawn(move || {
29+
| ^^^^^^^
30+
note: required by a bound in `spawn`
31+
(注釈: `spawn`の境界によって要求されます)
32+
--> /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/thread/mod.rs:678:1
1333

1434
For more information about this error, try `rustc --explain E0277`.
15-
error: could not compile `shared-state`.
16-
17-
To learn more, run the command again with --verbose.
35+
error: could not compile `shared-state` (bin "shared-state") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "shared-state"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "shared-state"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "threads"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

0 commit comments

Comments
 (0)