Skip to content

Commit 97d8397

Browse files
committed
ch15 スマートポインタの和訳を最新版に更新
rust-lang/book@19c40bf
1 parent 2296c28 commit 97d8397

File tree

62 files changed

+801
-1245
lines changed

Some content is hidden

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

62 files changed

+801
-1245
lines changed

dot/trpl15-04.dot

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
digraph {
2+
node[shape=record];
3+
rankdir=LR;
4+
5+
l1[label="{<data> 5| <next>}"];
6+
l2[label="{<data> 10| <next>}"];
7+
8+
{node[shape=point height=0] invisible_start invisible_end}
9+
10+
a -> l1:n;
11+
b -> l2:n;
12+
invisible_start:n -> l1[arrowtail=none];
13+
invisible_start:s -> invisible_end:s[dir=none];
14+
l1:next:c -> l2:data;
15+
l2:next:c -> invisible_end:n[arrowhead=none];
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "box-example"
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 = "cons-list"
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 = "cons-list"
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,28 +1,19 @@
11
$ cargo run
22
Compiling cons-list v0.1.0 (file:///projects/cons-list)
33
error[E0072]: recursive type `List` has infinite size
4+
(エラー: 再帰的な型`List`は無限のサイズを持ちます)
45
--> src/main.rs:1:1
56
|
67
1 | enum List {
7-
| ^^^^^^^^^ recursive type has infinite size
8+
| ^^^^^^^^^
89
2 | Cons(i32, List),
910
| ---- recursive without indirection
1011
|
11-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `List` representable
12-
13-
error[E0391]: cycle detected when processing `List`
14-
--> src/main.rs:1:1
15-
|
16-
1 | enum List {
17-
| ^^^^^^^^^
12+
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
13+
(ヘルプ: 何らかの間接参照(例: `Box`、`Rc`、あるいは`&`)を挿入して、循環参照を断ち切ってください)
1814
|
19-
= note: ...which again requires processing `List`, completing the cycle
20-
= note: cycle used when computing dropck types for `Canonical { max_universe: U0, variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing, def_id: None }, value: List } }`
21-
22-
error: aborting due to 2 previous errors
23-
24-
Some errors have detailed explanations: E0072, E0391.
25-
For more information about an error, try `rustc --explain E0072`.
26-
error: could not compile `cons-list`.
15+
2 | Cons(i32, Box<List>),
16+
| ++++ +
2717

28-
To learn more, run the command again with --verbose.
18+
For more information about this error, try `rustc --explain E0072`.
19+
error: could not compile `cons-list` (bin "cons-list") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "cons-list"
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 = "deref-example"
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 = "deref-example"
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 = "deref-example"
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 = "deref-example"
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,11 @@
11
$ cargo run
22
Compiling deref-example v0.1.0 (file:///projects/deref-example)
33
error[E0614]: type `MyBox<{integer}>` cannot be dereferenced
4+
(エラー: 型`MyBox<{integer}>`は参照外しできません)
45
--> src/main.rs:14:19
56
|
67
14 | assert_eq!(5, *y);
78
| ^^
89

9-
error: aborting due to previous error
10-
1110
For more information about this error, try `rustc --explain E0614`.
12-
error: could not compile `deref-example`.
13-
14-
To learn more, run the command again with --verbose.
11+
error: could not compile `deref-example` (bin "deref-example") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "deref-example"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch15-smart-pointers/listing-15-10/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::Deref;
44
impl<T> Deref for MyBox<T> {
55
type Target = T;
66

7-
fn deref(&self) -> &T {
7+
fn deref(&self) -> &Self::Target {
88
&self.0
99
}
1010
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "deref-example"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch15-smart-pointers/listing-15-11/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ANCHOR: here
22
fn hello(name: &str) {
3-
println!("Hello, {}!", name);
3+
println!("Hello, {name}!");
44
}
55
// ANCHOR_END: here
66

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

76
[dependencies]

listings/ch15-smart-pointers/listing-15-12/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<T> MyBox<T> {
1717
}
1818

1919
fn hello(name: &str) {
20-
println!("Hello, {}!", name);
20+
println!("Hello, {name}!");
2121
}
2222

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

76
[dependencies]

listings/ch15-smart-pointers/listing-15-13/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<T> MyBox<T> {
1717
}
1818

1919
fn hello(name: &str) {
20-
println!("Hello, {}!", name);
20+
println!("Hello, {name}!");
2121
}
2222

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

76
[dependencies]

listings/ch15-smart-pointers/listing-15-14/src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ struct CustomSmartPointer {
44

55
impl Drop for CustomSmartPointer {
66
fn drop(&mut self) {
7+
// "データ`{}`を持つCustomSmartPointerをドロップ中!"
78
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
89
}
910
}
1011

1112
fn main() {
1213
let c = CustomSmartPointer {
14+
// "俺のもの"
1315
data: String::from("my stuff"),
1416
};
1517
let d = CustomSmartPointer {
18+
// "別のもの"
1619
data: String::from("other stuff"),
1720
};
21+
// "CustomSmartPointerが作成された。"
1822
println!("CustomSmartPointers created.");
1923
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "drop-example"
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 drop-example v0.1.0 (file:///projects/drop-example)
33
error[E0040]: explicit use of destructor method
4+
(エラー: デストラクタメソッドを明示的に使用しています)
45
--> src/main.rs:16:7
56
|
67
16 | c.drop();
78
| ^^^^ explicit destructor calls not allowed
8-
9-
error: aborting due to previous error
9+
| (明示的なデストラクタの呼び出しは許可されません)
10+
|
11+
help: consider using `drop` function
12+
(ヘルプ: `drop`関数の使用を検討してください)
13+
|
14+
16 | drop(c);
15+
| +++++ ~
1016

1117
For more information about this error, try `rustc --explain E0040`.
12-
error: could not compile `drop-example`.
13-
14-
To learn more, run the command again with --verbose.
18+
error: could not compile `drop-example` (bin "drop-example") due to 1 previous error

listings/ch15-smart-pointers/listing-15-15/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
};
1616
println!("CustomSmartPointer created.");
1717
c.drop();
18+
// "CustomSmartPointerがmainの終端の前でドロップされた。"
1819
println!("CustomSmartPointer dropped before the end of main.");
1920
}
2021
// ANCHOR_END: here
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "drop-example"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch15-smart-pointers/listing-15-16/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
};
1616
println!("CustomSmartPointer created.");
1717
drop(c);
18+
// "CustomSmartPointerがmainの終端の前でドロップされた。"
1819
println!("CustomSmartPointer dropped before the end of main.");
1920
}
2021
// ANCHOR_END: here
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "cons-list"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch15-smart-pointers/listing-15-17/output.txt

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ error[E0382]: use of moved value: `a`
1010
11 | let c = Cons(4, Box::new(a));
1111
| ^ value used here after move
1212

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

76
[dependencies]

listings/ch15-smart-pointers/listing-15-19/src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ use std::rc::Rc;
99
// ANCHOR: here
1010
fn main() {
1111
let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
12+
// "a作成後のカウント = {}"
1213
println!("count after creating a = {}", Rc::strong_count(&a));
1314
let b = Cons(3, Rc::clone(&a));
15+
// "b作成後のカウント = {}"
1416
println!("count after creating b = {}", Rc::strong_count(&a));
1517
{
1618
let c = Cons(4, Rc::clone(&a));
19+
// "c作成後のカウント = {}"
1720
println!("count after creating c = {}", Rc::strong_count(&a));
1821
}
22+
// "cがスコープを抜けた後のカウント = {}"
1923
println!("count after c goes out of scope = {}", Rc::strong_count(&a));
2024
}
2125
// ANCHOR_END: here
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "limit-tracker"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch15-smart-pointers/listing-15-20/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl<'a, T> LimitTracker<'a, T>
1212
where
1313
T: Messenger,
1414
{
15-
pub fn new(messenger: &T, max: usize) -> LimitTracker<T> {
15+
pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> {
1616
LimitTracker {
1717
messenger,
1818
value: 0,
@@ -26,11 +26,14 @@ where
2626
let percentage_of_max = self.value as f64 / self.max as f64;
2727

2828
if percentage_of_max >= 1.0 {
29+
// "エラー: 割り当てを超えています!"
2930
self.messenger.send("Error: You are over your quota!");
3031
} else if percentage_of_max >= 0.9 {
32+
// "緊急警告: 割り当ての90%以上を使用してしまいました!"
3133
self.messenger
3234
.send("Urgent warning: You've used up over 90% of your quota!");
3335
} else if percentage_of_max >= 0.75 {
36+
// "警告: 割り当ての75%以上を使用してしまいました!"
3437
self.messenger
3538
.send("Warning: You've used up over 75% of your quota!");
3639
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "limit-tracker"
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)