1+ <!--
12# Higher-Rank Trait Bounds (HRTBs)
3+ -->
24
5+ # 高階トレイト境界
6+
7+ <!--
38Rust's `Fn` traits are a little bit magic. For instance, we can write the
49following code:
10+ -->
11+
12+ Rust の ` Fn ` トレイトはちょっとした魔法です。例えば、次のように書くことができます。
513
614``` rust
715struct Closure <F > {
@@ -25,8 +33,12 @@ fn main() {
2533}
2634```
2735
36+ <!--
2837If we try to naively desugar this code in the same way that we did in the
2938lifetimes section, we run into some trouble:
39+ -->
40+
41+ ライフタイムの節と同じように単純に脱糖しようとすると、問題が起こります。
3042
3143``` rust,ignore
3244struct Closure<F> {
@@ -52,22 +64,42 @@ fn main() {
5264}
5365```
5466
67+ <!--
5568How on earth are we supposed to express the lifetimes on `F`'s trait bound? We
5669need to provide some lifetime there, but the lifetime we care about can't be
5770named 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+ <!--
6079This job requires The Magic of Higher-Rank Trait Bounds (HRTBs). The way we
6180desugar this is as follows:
81+ -->
82+
83+ この問題は、高階トレイト境界(HRTB: Higher-Rank Trait Bounds)という魔法で解決できます。
84+ HRTB を使うとつぎのように脱糖できます。
6285
6386``` rust,ignore
6487where 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`
6892trait)
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
72100places outside of the `Fn` traits where we encounter HRTBs, and even for
73101those we have a nice magic sugar for the common cases.
102+ -->
103+
104+ ` for<'a> ` は、「` 'a ` に何を選んだとしても」という意味で、つまり F が満たさなくてはならないトレイト境界の* 無限リスト* を生成します。強烈でしょう?
105+ ` Fn ` トレイトを除けば、HRTB が使われる場所はほとんどありません。` Fn ` トレイトにおいても、ほとんどの場合は魔法の糖衣構文が良いされています。
0 commit comments