|
| 1 | +<!-- |
1 | 2 | # Lifetime Elision
|
| 3 | +--> |
2 | 4 |
|
| 5 | +# 生存期間の省略 |
| 6 | + |
| 7 | +<!-- |
3 | 8 | In order to make common patterns more ergonomic, Rust allows lifetimes to be
|
4 | 9 | *elided* in function signatures.
|
| 10 | +--> |
| 11 | + |
| 12 | +よくあるパターンをより易しく書けるように、Rust では関数シグネチャの生存期間を省略できます。 |
5 | 13 |
|
| 14 | +<!-- |
6 | 15 | A *lifetime position* is anywhere you can write a lifetime in a type:
|
| 16 | +--> |
| 17 | + |
| 18 | +*生存期間ポジション* とは、型の定義において生存期間を書ける場所のことです。 |
7 | 19 |
|
8 | 20 | ```rust,ignore
|
9 | 21 | &'a T
|
10 | 22 | &'a mut T
|
11 | 23 | T<'a>
|
12 | 24 | ```
|
13 | 25 |
|
| 26 | +<!-- |
14 | 27 | Lifetime positions can appear as either "input" or "output":
|
| 28 | +--> |
15 | 29 |
|
| 30 | +生存期間ポジションは、「入力」か「出力」のいづれかです。 |
| 31 | + |
| 32 | +<!-- |
16 | 33 | * For `fn` definitions, input refers to the types of the formal arguments
|
17 | 34 | in the `fn` definition, while output refers to
|
18 | 35 | result types. So `fn foo(s: &str) -> (&str, &str)` has elided one lifetime in
|
19 | 36 | input position and two lifetimes in output position.
|
20 | 37 | Note that the input positions of a `fn` method definition do not
|
21 | 38 | include the lifetimes that occur in the method's `impl` header
|
22 | 39 | (nor lifetimes that occur in the trait header, for a default method).
|
| 40 | +--> |
| 41 | + |
| 42 | +* `fn` 定義では、入力とは仮引数の型のことで、出力とは結果の型のことです。 |
| 43 | + `fn foo(s: *str) -> (&str, &str)` では、入力ポジションの生存期間が一つ省略され、 |
| 44 | + 出力ポジションの生存期間が二つ省略されています。 |
| 45 | + `fn` メソッド定義の入力ポジションには、 |
| 46 | + メソッドの `impl` ヘッダに現れる生存期間は含まれません。 |
| 47 | + (デフォルトメソッドの場合の trait ヘッダに現れる生存期間も含まれません。) |
23 | 48 |
|
| 49 | +<!-- |
24 | 50 | * In the future, it should be possible to elide `impl` headers in the same manner.
|
| 51 | +--> |
| 52 | + |
| 53 | +* 将来のバージョンでは、`impl` ヘッダの生存期間の省略も同様に可能になるでしょう。 |
25 | 54 |
|
| 55 | +<!-- |
26 | 56 | Elision rules are as follows:
|
| 57 | +--> |
27 | 58 |
|
| 59 | +省略のルールは次の通りです。 |
| 60 | + |
| 61 | +<!-- |
28 | 62 | * Each elided lifetime in input position becomes a distinct lifetime
|
29 | 63 | parameter.
|
| 64 | +--> |
| 65 | + |
| 66 | +* 入力ポジションの省略された生存期間は、それぞれ別の生存期間パラメタになります。 |
30 | 67 |
|
| 68 | +<!-- |
31 | 69 | * If there is exactly one input lifetime position (elided or not), that lifetime
|
32 | 70 | is assigned to *all* elided output lifetimes.
|
| 71 | +--> |
| 72 | + |
| 73 | +* 入力ポジションの生存期間(省略されているかどうかに関わらず)が一つしか無い場合、 |
| 74 | + 省略された出力生存期間全てにその生存期間が割り当てられます。 |
33 | 75 |
|
| 76 | +<!-- |
34 | 77 | * If there are multiple input lifetime positions, but one of them is `&self` or
|
35 | 78 | `&mut self`, the lifetime of `self` is assigned to *all* elided output lifetimes.
|
| 79 | +--> |
36 | 80 |
|
| 81 | +* 入力ポジションに複数の生存期間があって、そのうちの一つが `&self` または `&mut self` の場合、 |
| 82 | + 省略された出力生存期間全てに `self` の生存期間が割り当てられます。 |
| 83 | + |
| 84 | +<!-- |
37 | 85 | * Otherwise, it is an error to elide an output lifetime.
|
| 86 | +--> |
| 87 | + |
| 88 | +* それ以外の場合は、出力の生存期間を省略するとエラーになります。 |
38 | 89 |
|
| 90 | +<!-- |
39 | 91 | Examples:
|
| 92 | +--> |
| 93 | + |
| 94 | +例: |
40 | 95 |
|
41 | 96 | ```rust,ignore
|
42 |
| -fn print(s: &str); // elided |
43 |
| -fn print<'a>(s: &'a str); // expanded |
| 97 | +fn print(s: &str); // 省略した場合 |
| 98 | +fn print<'a>(s: &'a str); // 展開した場合 |
44 | 99 |
|
45 |
| -fn debug(lvl: uint, s: &str); // elided |
46 |
| -fn debug<'a>(lvl: uint, s: &'a str); // expanded |
| 100 | +fn debug(lvl: uint, s: &str); // 省略した場合 |
| 101 | +fn debug<'a>(lvl: uint, s: &'a str); // 展開した場合 |
47 | 102 |
|
48 |
| -fn substr(s: &str, until: uint) -> &str; // elided |
49 |
| -fn substr<'a>(s: &'a str, until: uint) -> &'a str; // expanded |
| 103 | +fn substr(s: &str, until: uint) -> &str; // 省略した場合 |
| 104 | +fn substr<'a>(s: &'a str, until: uint) -> &'a str; // 展開した場合 |
50 | 105 |
|
51 |
| -fn get_str() -> &str; // ILLEGAL |
| 106 | +fn get_str() -> &str; // エラー |
52 | 107 |
|
53 |
| -fn frob(s: &str, t: &str) -> &str; // ILLEGAL |
| 108 | +fn frob(s: &str, t: &str) -> &str; // エラー |
54 | 109 |
|
55 |
| -fn get_mut(&mut self) -> &mut T; // elided |
56 |
| -fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded |
| 110 | +fn get_mut(&mut self) -> &mut T; // 省略した場合 |
| 111 | +fn get_mut<'a>(&'a mut self) -> &'a mut T; // 展開した場合 |
57 | 112 |
|
58 |
| -fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command // elided |
59 |
| -fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // expanded |
| 113 | +fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command // 省略した場合 |
| 114 | +fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // 展開した場合 |
60 | 115 |
|
61 |
| -fn new(buf: &mut [u8]) -> BufWriter; // elided |
62 |
| -fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // expanded |
| 116 | +fn new(buf: &mut [u8]) -> BufWriter; // 省略した場合 |
| 117 | +fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // 展開した場合 |
63 | 118 |
|
64 | 119 | ```
|
0 commit comments