Skip to content

Commit 03898a9

Browse files
committed
Squashed commit of the following:
commit 0d8b83777b33c7164d31fea0c3a310531213d09b Author: Ken Kawamoto <kentaro.kawamoto@gmail.com> Date: Mon May 29 12:34:36 2017 -0700 chapter 3.6
1 parent 18bdba8 commit 03898a9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* [ライフタイムシステムの限界](lifetime-mismatch.md)
1616
* [ライフタイムの省略](lifetime-elision.md)
1717
* [無制限のライフタイム](unbounded-lifetimes.md)
18-
* [Higher-Rank Trait Bounds](hrtb.md)
18+
* [高階トレイト境界](hrtb.md)
1919
* [Subtyping and Variance](subtyping.md)
2020
* [Drop Check](dropck.md)
2121
* [PhantomData](phantom-data.md)

src/hrtb.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
<!--
12
# Higher-Rank Trait Bounds (HRTBs)
3+
-->
24

5+
# 高階トレイト境界
6+
7+
<!--
38
Rust's `Fn` traits are a little bit magic. For instance, we can write the
49
following code:
10+
-->
11+
12+
Rust の `Fn` トレイトはちょっとした魔法です。例えば、次のように書くことができます。
513

614
```rust
715
struct Closure<F> {
@@ -25,8 +33,12 @@ fn main() {
2533
}
2634
```
2735

36+
<!--
2837
If we try to naively desugar this code in the same way that we did in the
2938
lifetimes section, we run into some trouble:
39+
-->
40+
41+
ライフタイムの節と同じように単純に脱糖しようとすると、問題が起こります。
3042

3143
```rust,ignore
3244
struct Closure<F> {
@@ -52,22 +64,42 @@ fn main() {
5264
}
5365
```
5466

67+
<!--
5568
How on earth are we supposed to express the lifetimes on `F`'s trait bound? We
5669
need to provide some lifetime there, but the lifetime we care about can't be
5770
named until we enter the body of `call`! Also, that isn't some fixed lifetime;
5871
`call` works with *any* lifetime `&self` happens to have at that point.
72+
-->
73+
74+
`F` のトレイト境界は、一体どうすれば表現できるのでしょう?
75+
なんらかのライフタイムを提供する必要がありますが、問題のライフタイムは `call` 関数が呼ばれるまで名前が無いのです。さらに、ライフタイムは固定されていません。
76+
`&self`*どんな*ライフタイムが割り当てられても、`call` は動作します。
5977

78+
<!--
6079
This job requires The Magic of Higher-Rank Trait Bounds (HRTBs). The way we
6180
desugar this is as follows:
81+
-->
82+
83+
この問題は、高階トレイト境界(HRTB: Higher-Rank Trait Bounds)という魔法で解決できます。
84+
HRTB を使うとつぎのように脱糖できます。
6285

6386
```rust,ignore
6487
where for<'a> F: Fn(&'a (u8, u16)) -> &'a u8,
6588
```
6689

90+
<!--
6791
(Where `Fn(a, b, c) -> d` is itself just sugar for the unstable *real* `Fn`
6892
trait)
93+
-->
94+
95+
`Fn(a, b, c) -> d` 自体が、まだ仕様が安定していない*本当の* `Fn` トレイトの糖衣構文です。)
6996

97+
<!--
7098
`for<'a>` can be read as "for all choices of `'a`", and basically produces an
7199
*infinite list* of trait bounds that F must satisfy. Intense. There aren't many
72100
places outside of the `Fn` traits where we encounter HRTBs, and even for
73101
those we have a nice magic sugar for the common cases.
102+
-->
103+
104+
`for<'a>` は、「`'a` に何を選んだとしても」という意味で、つまり F が満たさなくてはならないトレイト境界の*無限リスト*を生成します。強烈でしょう?
105+
`Fn` トレイトを除けば、HRTB が使われる場所はほとんどありません。`Fn` トレイトにおいても、ほとんどの場合は魔法の糖衣構文が良いされています。

0 commit comments

Comments
 (0)