Skip to content

Commit 62928c0

Browse files
authored
Merge pull request #85 from blueblade/translate
「6. 型変換」などを翻訳
2 parents 2e81bed + a8356ee commit 62928c0

File tree

19 files changed

+198
-21
lines changed

19 files changed

+198
-21
lines changed

src/SUMMARY.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
- [Declare first](variable_bindings/declare.md)
5555
- [Freezing](variable_bindings/freeze.md)
5656
-->
57-
- [変数バインディング](variable_bindings.md)
57+
- [変数束縛](variable_bindings.md)
5858
- [ミュータビリティ](variable_bindings/mut.md)
5959
- [スコープとシャドーイング](variable_bindings/scope.md)
6060
- [宣言](variable_bindings/declare.md)
61-
- [値のフリーズ](variable_bindings/freeze.md)
61+
- [値の凍結](variable_bindings/freeze.md)
6262

6363
<!--
6464
- [Types](types.md)
@@ -67,16 +67,22 @@
6767
- [Inference](types/inference.md)
6868
- [Aliasing](types/alias.md)
6969
-->
70-
- [Types](types.md)
70+
- [](types.md)
7171
- [型キャスティング](types/cast.md)
7272
- [リテラル](types/literals.md)
7373
- [型推論](types/inference.md)
7474
- [エイリアス](types/alias.md)
7575

76+
<!--
7677
- [Conversion](conversion.md)
7778
- [`From` and `Into`](conversion/from_into.md)
7879
- [`TryFrom` and `TryInto`](conversion/try_from_try_into.md)
7980
- [To and from `String`s](conversion/string.md)
81+
-->
82+
- [型変換](conversion.md)
83+
- [`From`および`Into`](conversion/from_into.md)
84+
- [`TryFrom`および`TryInto`](conversion/try_from_try_into.md)
85+
- [`String`との型変換](conversion/string.md)
8086

8187
<!--
8288
- [Expressions](expression.md)
@@ -106,7 +112,7 @@
106112
- [if/else](flow_control/if_else.md)
107113
- [loop](flow_control/loop.md)
108114
- [ネストとラベル](flow_control/loop/nested.md)
109-
- [Returning from loops](flow_control/loop/return.md)
115+
- [loopが返す値](flow_control/loop/return.md)
110116
- [while](flow_control/while.md)
111117
- [for と range](flow_control/for.md)
112118
- [match](flow_control/match.md)

src/conversion.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
<!--
12
# Conversion
3+
-->
4+
# 型変換
25

6+
<!--
37
Rust addresses conversion between types by the use of [traits]. The generic
48
conversions will use the [`From`] and [`Into`] traits. However there are more
59
specific ones for the more common cases, in particular when converting to and
610
from `String`s.
11+
-->
12+
Rustは型の変換を[トレイト][traits]を用いて行います。ジェネリックな型変換には[`From`]および[`Into`]トレイトを使用します。しかし、よくあるケースにおいて、特に`String`との相互の型変換では、特殊なトレイトが使用されます。
713

814
[traits]: trait.md
915
[`From`]: https://doc.rust-lang.org/std/convert/trait.From.html

src/conversion/from_into.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1+
<!--
12
# `From` and `Into`
3+
-->
4+
# `From`および`Into`
25

6+
<!--
37
The [`From`] and [`Into`] traits are inherently linked, and this is actually part of
48
its implementation. If you are able to convert type A from type B, then it
59
should be easy to believe that we should be able to convert type B to type A.
10+
-->
11+
[`From`]トレイトと[`Into`]トレイトは本質的に結びついており、そのことが実際に実装に反映されています。もし型Aから型Bへの変換ができるのであれば、型Bから型Aへの変換もできると思うのが自然です。
612

713
## `From`
814

15+
<!--
916
The [`From`] trait allows for a type to define how to create itself from another
1017
type, hence providing a very simple mechanism for converting between several
1118
types. There are numerous implementations of this trait within the standard
1219
library for conversion of primitive and common types.
20+
-->
21+
[`From`]トレイトは、ある型に対し、別の型からその型を作る方法を定義できるようにするものです。そのため、複数の型の間で型変換を行うための非常にシンプルな仕組みを提供しています。標準ライブラリでは、基本データ型やよく使われる型に対して、このトレイトが多数実装されています。
1322

23+
<!--
1424
For example we can easily convert a `str` into a `String`
25+
-->
26+
例えば、`str`から`String`への型変換は簡単です。
1527

1628
```rust
1729
let my_str = "hello";
1830
let my_string = String::from(my_str);
1931
```
2032

33+
<!--
2134
We can do similar for defining a conversion for our own type.
35+
-->
36+
自作の型に対しても、型変換を定義すれば同じように行えます。
2237

2338
```rust,editable
2439
use std::convert::From;
@@ -42,13 +57,19 @@ fn main() {
4257

4358
## `Into`
4459

60+
<!--
4561
The [`Into`] trait is simply the reciprocal of the `From` trait. That is, if you
4662
have implemented the `From` trait for your type, `Into` will call it when
4763
necessary.
64+
-->
65+
[`Into`]トレイトは、単に`From`トレイトの逆の働きをします。もし自作の型に`From`トレイトが実装されていたら、`Into`は必要に応じてそれを呼び出します。
4866

67+
<!--
4968
Using the `Into` trait will typically require specification of the type to
5069
convert into as the compiler is unable to determine this most of the time.
5170
However this is a small trade-off considering we get the functionality for free.
71+
-->
72+
`Into`トレイトを使用すると、ほとんどの場合、コンパイラが型を決定することができないため、変換する型を指定する必要があります。しかし、この機能を無料で得られることを考えれば、これは小さなトレードオフです。
5273

5374
```rust,editable
5475
use std::convert::From;

src/conversion/string.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
<!--
12
# To and from Strings
3+
-->
4+
# Stringとの型変換
25

6+
<!--
37
## Converting to String
8+
-->
9+
## Stringへの型変換
410

11+
<!--
512
To convert any type to a `String` is as simple as implementing the [`ToString`]
613
trait for the type. Rather than doing so directly, you should implement the
714
[`fmt::Display`][Display] trait which automagically provides [`ToString`] and
815
also allows printing the type as discussed in the section on [`print!`][print].
16+
-->
17+
任意の型を`String`に変換するのは簡単で、その型に[`ToString`]トレイトを実装するだけです。これを直接実装するよりも、[`fmt::Display`][Display]トレイトを実装するのがよいでしょう。そうすることで自動的に[`ToString`]が提供されるだけでなく、[`print!`][print]の章で説明したように、その型を表示できるようにもなります。
918

1019
```rust,editable
1120
use std::fmt;
@@ -26,17 +35,26 @@ fn main() {
2635
}
2736
```
2837

38+
<!--
2939
## Parsing a String
40+
-->
41+
## Stringの解析
3042

43+
<!--
3144
One of the more common types to convert a string into is a number. The idiomatic
3245
approach to this is to use the [`parse`] function and either to arrange for
3346
type inference or to specify the type to parse using the 'turbofish' syntax.
3447
Both alternatives are shown in the following example.
48+
-->
49+
文字列からの型変換において、数値への型変換はよく行われるものの一つです。これを行うイディオムは[`parse`]関数を使用することですが、このときに型を推論できるようにするか、もしくは turbofish構文を使用して型を指定するかのいずれかを行います。以下の例では、どちらの方法も紹介しています。
3550

51+
<!--
3652
This will convert the string into the type specified so long as the [`FromStr`]
3753
trait is implemented for that type. This is implemented for numerous types
3854
within the standard library. To obtain this functionality on a user defined type
3955
simply implement the [`FromStr`] trait for that type.
56+
-->
57+
`parse`関数は、指定された型に`FromStr`トレイトが実装されていれば、文字列をその型に変換します。このトレイトは標準ライブラリの多くの型に対して実装されています。ユーザー定義の型でこの機能を利用するには、その型に対して`FromStr`トレイトを実装するだけです。
4058

4159
```rust,editable
4260
fn main() {

src/conversion/try_from_try_into.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
<!--
12
# `TryFrom` and `TryInto`
3+
-->
4+
# `TryFrom`および`TryInto`
25

6+
<!--
37
Similar to [`From` and `Into`][from-into], [`TryFrom`] and [`TryInto`] are
48
generic traits for converting between types. Unlike `From`/`Into`, the
59
`TryFrom`/`TryInto` traits are used for fallible conversions, and as such,
610
return [`Result`]s.
11+
-->
12+
[`From`および`Into`][from-into]と同様に、[`TryFrom`]および[`TryInto`]も型変換を行うジェネリックなトレイトです。`From`/`Into`と異なり、`TryFrom`/`TryInto`トレイトは失敗する可能性のある型変換に用いられるので、[`Result`]を返します。
13+
714

815
[from-into]: from_into.html
916
[`TryFrom`]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html

src/custom_types/enum.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,35 @@ The `enum` keyword allows the creation of a type which may be one of a few
88
different variants. Any variant which is valid as a `struct` is also valid as
99
an `enum`.
1010
-->
11-
列挙型(`enum`)はいくつかの異なる型の中から1つを選ぶような場合に使用する。構造体(`struct`)の定義を満たすものならば何でも`enum` 内の型として使用できる
11+
列挙型(`enum`)はいくつかの異なる要素型の中から1つを選ぶような場合に使用します。構造体(`struct`)の定義を満たすものならば何でも`enum` の要素型として使用できます。
1212

1313
```rust,editable
1414
// Create an `enum` to classify a web event. Note how both
1515
// names and type information together specify the variant:
1616
// `PageLoad != PageUnload` and `KeyPress(char) != Paste(String)`.
1717
// Each is different and independent.
18+
// `enum`を作成してwebイベントを分類する。
19+
// 名前と型情報を併せたものが要素型になっていることに注意。
20+
// `PageLoad != PageUnload` であり、
21+
// `KeyPress(char) != Paste(String)` である。
22+
// 要素型は互いに異なり、互いに非依存である。
1823
enum WebEvent {
1924
// An `enum` may either be `unit-like`,
25+
// `enum`要素型はユニット風でもよい
2026
PageLoad,
2127
PageUnload,
2228
// like tuple structs,
29+
// タプル風でもよい
2330
KeyPress(char),
2431
Paste(String),
2532
// or c-like structures.
33+
// C言語スタイルの構造体風でもよい
2634
Click { x: i64, y: i64 },
2735
}
2836
2937
// A function which takes a `WebEvent` enum as an argument and
3038
// returns nothing.
39+
// 引数として`WebEvent`列挙型をとり、何も返さない関数
3140
fn inspect(event: WebEvent) {
3241
match event {
3342
WebEvent::PageLoad => println!("page loaded"),
@@ -45,6 +54,7 @@ fn inspect(event: WebEvent) {
4554
fn main() {
4655
let pressed = WebEvent::KeyPress('x');
4756
// `to_owned()` creates an owned `String` from a string slice.
57+
// `to_owned()`は文字列スライスから所有権のある`String`を作成する
4858
let pasted = WebEvent::Paste("my text".to_owned());
4959
let click = WebEvent::Click { x: 20, y: 80 };
5060
let load = WebEvent::PageLoad;
@@ -59,11 +69,17 @@ fn main() {
5969
6070
```
6171

72+
<!--
6273
## Type aliases
74+
-->
75+
## 型エイリアス
6376

77+
<!--
6478
If you use a type alias, you can refer to each enum variant via its alias.
6579
This might be useful if the enum's name is too long or too generic, and you
6680
want to rename it.
81+
-->
82+
型エイリアスを用いると、列挙型の要素型を別名で参照できます。これは列挙型の名前があまりに長かったり、あまりに一般的だったりで改名したい場合に役立ちます。
6783

6884
```rust,editable
6985
enum VeryVerboseEnumOfThingsToDoWithNumbers {
@@ -72,16 +88,21 @@ enum VeryVerboseEnumOfThingsToDoWithNumbers {
7288
}
7389
7490
// Creates a type alias
91+
// 型エイリアスを作成する
7592
type Operations = VeryVerboseEnumOfThingsToDoWithNumbers;
7693
7794
fn main() {
7895
// We can refer to each variant via its alias, not its long and inconvenient
7996
// name.
97+
// 長くて不便な列挙型の名前ではなく、別名を使って要素型を参照できる
8098
let x = Operations::Add;
8199
}
82100
```
83101

102+
<!--
84103
The most common place you'll see this is in `impl` blocks using the `Self` alias.
104+
-->
105+
このやり方がもっともよく見られるのは、`impl`ブロックで`Self`という別名を使用する場合です。
85106

86107
```rust,editable
87108
enum VeryVerboseEnumOfThingsToDoWithNumbers {
@@ -99,9 +120,12 @@ impl VeryVerboseEnumOfThingsToDoWithNumbers {
99120
}
100121
```
101122

123+
<!--
102124
To learn more about enums and type aliases, you can read the
103125
[stabilization report][aliasreport] from when this feature was stabilized into
104126
Rust.
127+
-->
128+
列挙型や型エイリアスについて詳しく学びたい人は、この機能が安定してRustに取り込まれた後に[stabilization report][aliasreport]を読んでください。
105129

106130
<!--
107131
### See also:

src/custom_types/structs.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,25 @@ struct Point {
4747
struct Rectangle {
4848
// A rectangle can be specified by where the top left and bottom right
4949
// corners are in space.
50+
// 長方形は座標空間上における左上隅と右下隅の位置によって指定できる
5051
top_left: Point,
5152
bottom_right: Point,
5253
}
5354
5455
fn main() {
5556
// Create struct with field init shorthand
57+
// 構造体をフィールド初期化の簡略記法で生成
5658
let name = "Peter";
5759
let age = 27;
5860
let peter = Person { name, age };
5961
6062
// Print debug struct
63+
// 構造体のデバッグ表示を行う
6164
println!("{:?}", peter);
6265
6366
6467
// Instantiate a `Point`
68+
// `Point` のインスタンス化
6569
let point: Point = Point { x: 10.3, y: 0.4 };
6670
6771
// Access the fields of the point
@@ -70,10 +74,14 @@ fn main() {
7074
7175
// Make a new point by using struct update syntax to use the fields of our
7276
// other one
77+
// 構造体の更新記法を用いて、別の構造体のフィールドの値を基に
78+
// 新たなpointを生成
7379
let bottom_right = Point { x: 5.2, ..point };
7480
7581
// `bottom_right.y` will be the same as `point.y` because we used that field
7682
// from `point`
83+
// `bottom_right.y` の値は `point.y` と同一になるが、
84+
// これは `point` のフィールドの値を用いて生成したためである
7785
println!("second point: ({}, {})", bottom_right.x, bottom_right.y);
7886
7987
// Destructure the point using a `let` binding
@@ -96,6 +104,7 @@ fn main() {
96104
let pair = Pair(1, 0.1);
97105
98106
// Access the fields of a tuple struct
107+
// タプルのフィールドにアクセス
99108
println!("pair contains {:?} and {:?}", pair.0, pair.1);
100109
101110
// Destructure a tuple struct

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy