Skip to content

Commit d57fbce

Browse files
committed
Squashed commit of the following:
commit 00042f87f75601980e134e91401ba8e7bfd23ec7 Author: Ken Kawamoto <kentaro.kawamoto@gmail.com> Date: Sun Apr 16 20:29:34 2017 -0700 chapter 3.4 commit 08dba2e6ed76855352725c973f32d58845f0871d Author: Ken Kawamoto <kentaro.kawamoto@gmail.com> Date: Sun Apr 16 15:34:39 2017 -0700 started chapter 3.4
1 parent 7c1d017 commit d57fbce

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* [リファレンス](references.md)
1414
* [生存期間](lifetimes.md)
1515
* [生存期間システムの限界](lifetime-mismatch.md)
16-
* [Lifetime Elision](lifetime-elision.md)
16+
* [生存期間の省略](lifetime-elision.md)
1717
* [Unbounded Lifetimes](unbounded-lifetimes.md)
1818
* [Higher-Rank Trait Bounds](hrtb.md)
1919
* [Subtyping and Variance](subtyping.md)

src/lifetime-elision.md

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,119 @@
1+
<!--
12
# Lifetime Elision
3+
-->
24

5+
# 生存期間の省略
6+
7+
<!--
38
In order to make common patterns more ergonomic, Rust allows lifetimes to be
49
*elided* in function signatures.
10+
-->
11+
12+
よくあるパターンをより易しく書けるように、Rust では関数シグネチャの生存期間を省略できます。
513

14+
<!--
615
A *lifetime position* is anywhere you can write a lifetime in a type:
16+
-->
17+
18+
*生存期間ポジション* とは、型の定義において生存期間を書ける場所のことです。
719

820
```rust,ignore
921
&'a T
1022
&'a mut T
1123
T<'a>
1224
```
1325

26+
<!--
1427
Lifetime positions can appear as either "input" or "output":
28+
-->
1529

30+
生存期間ポジションは、「入力」か「出力」のいづれかです。
31+
32+
<!--
1633
* For `fn` definitions, input refers to the types of the formal arguments
1734
in the `fn` definition, while output refers to
1835
result types. So `fn foo(s: &str) -> (&str, &str)` has elided one lifetime in
1936
input position and two lifetimes in output position.
2037
Note that the input positions of a `fn` method definition do not
2138
include the lifetimes that occur in the method's `impl` header
2239
(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 ヘッダに現れる生存期間も含まれません。)
2348

49+
<!--
2450
* In the future, it should be possible to elide `impl` headers in the same manner.
51+
-->
52+
53+
* 将来のバージョンでは、`impl` ヘッダの生存期間の省略も同様に可能になるでしょう。
2554

55+
<!--
2656
Elision rules are as follows:
57+
-->
2758

59+
省略のルールは次の通りです。
60+
61+
<!--
2862
* Each elided lifetime in input position becomes a distinct lifetime
2963
parameter.
64+
-->
65+
66+
* 入力ポジションの省略された生存期間は、それぞれ別の生存期間パラメタになります。
3067

68+
<!--
3169
* If there is exactly one input lifetime position (elided or not), that lifetime
3270
is assigned to *all* elided output lifetimes.
71+
-->
72+
73+
* 入力ポジションの生存期間(省略されているかどうかに関わらず)が一つしか無い場合、
74+
省略された出力生存期間全てにその生存期間が割り当てられます。
3375

76+
<!--
3477
* If there are multiple input lifetime positions, but one of them is `&self` or
3578
`&mut self`, the lifetime of `self` is assigned to *all* elided output lifetimes.
79+
-->
3680

81+
* 入力ポジションに複数の生存期間があって、そのうちの一つが `&self` または `&mut self` の場合、
82+
省略された出力生存期間全てに `self` の生存期間が割り当てられます。
83+
84+
<!--
3785
* Otherwise, it is an error to elide an output lifetime.
86+
-->
87+
88+
* それ以外の場合は、出力の生存期間を省略するとエラーになります。
3889

90+
<!--
3991
Examples:
92+
-->
93+
94+
例:
4095

4196
```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); // 展開した場合
4499
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); // 展開した場合
47102
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; // 展開した場合
50105
51-
fn get_str() -> &str; // ILLEGAL
106+
fn get_str() -> &str; // エラー
52107
53-
fn frob(s: &str, t: &str) -> &str; // ILLEGAL
108+
fn frob(s: &str, t: &str) -> &str; // エラー
54109
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; // 展開した場合
57112
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 // 展開した場合
60115
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> // 展開した場合
63118
64119
```

0 commit comments

Comments
 (0)