-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ja: Translate Chapter 25 (Traits) #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
e91ef16
a5f938f
cb4b5d6
81e2db7
a723f7e
20cbf7e
bf53b7c
addcf29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8008,6 +8008,8 @@ msgstr "" | |
msgid "" | ||
"Rust lets you abstract over types with traits. They're similar to interfaces:" | ||
msgstr "" | ||
"Rustでは、型に関しての抽象化をトレイトを用いて行うことができます。トレイトは" | ||
"interfaceに似ています:" | ||
|
||
#: src/traits.md:5 | ||
msgid "" | ||
|
@@ -8041,12 +8043,43 @@ msgid "" | |
"}\n" | ||
"```" | ||
msgstr "" | ||
"```rust,editable\n" | ||
"struct Dog { name: String, age: i8 }\n" | ||
"struct Cat { lives: i8 } // 猫に名前は必要ありません。どのみち猫は名前に反応" | ||
"しないからです。\n" | ||
"\n" | ||
"trait Pet {\n" | ||
" fn talk(&self) -> String;\n" | ||
"}\n" | ||
"\n" | ||
"impl Pet for Dog {\n" | ||
" fn talk(&self) -> String { format!(\"Woof, my name is {}!\", self." | ||
"name) }\n" | ||
"}\n" | ||
"\n" | ||
"impl Pet for Cat {\n" | ||
" fn talk(&self) -> String { String::from(\"Miau!\") }\n" | ||
"}\n" | ||
"\n" | ||
"fn greet<P: Pet>(pet: &P) {\n" | ||
" println!(\"Oh you're a cutie! What's your name? {}\", pet.talk());\n" | ||
"}\n" | ||
"\n" | ||
"fn main() {\n" | ||
" let captain_floof = Cat { lives: 9 };\n" | ||
" let fido = Dog { name: String::from(\"Fido\"), age: 5 };\n" | ||
"\n" | ||
" greet(&captain_floof);\n" | ||
" greet(&fido);\n" | ||
"}\n" | ||
"```" | ||
|
||
#: src/traits/trait-objects.md:3 | ||
msgid "" | ||
"Trait objects allow for values of different types, for instance in a " | ||
"collection:" | ||
msgstr "" | ||
"トレイトオブジェクトは異なる型の値を許可します。例をまとめて挙げるとすると:" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/trait-objects.md:5 | ||
msgid "" | ||
|
@@ -8078,10 +8111,38 @@ msgid "" | |
"}\n" | ||
"```" | ||
msgstr "" | ||
"```rust,editable\n" | ||
"struct Dog { name: String, age: i8 }\n" | ||
"struct Cat { lives: i8 } // 猫に名前は必要ありません。どのみち猫は名前に反応" | ||
"しないからです。\n" | ||
"\n" | ||
"trait Pet {\n" | ||
" fn talk(&self) -> String;\n" | ||
"}\n" | ||
"\n" | ||
"impl Pet for Dog {\n" | ||
" fn talk(&self) -> String { format!(\"Woof, my name is {}!\", self." | ||
"name) }\n" | ||
"}\n" | ||
"\n" | ||
"impl Pet for Cat {\n" | ||
" fn talk(&self) -> String { String::from(\"Miau!\") }\n" | ||
"}\n" | ||
"\n" | ||
"fn main() {\n" | ||
" let pets: Vec<Box<dyn Pet>> = vec![\n" | ||
" Box::new(Cat { lives: 9 }),\n" | ||
" Box::new(Dog { name: String::from(\"Fido\"), age: 5 }),\n" | ||
" ];\n" | ||
" for pet in pets {\n" | ||
" println!(\"Hello, who are you? {}\", pet.talk());\n" | ||
" }\n" | ||
"}\n" | ||
"```" | ||
|
||
#: src/traits/trait-objects.md:32 | ||
msgid "Memory layout after allocating `pets`:" | ||
msgstr "" | ||
msgstr "`pets`を割り当てた後のメモリレイアウト:" | ||
|
||
#: src/traits/trait-objects.md:34 | ||
msgid "" | ||
|
@@ -8149,18 +8210,25 @@ msgid "" | |
"Types that implement a given trait may be of different sizes. This makes it " | ||
"impossible to have things like `Vec<dyn Pet>` in the example above." | ||
msgstr "" | ||
"与えられたトレイトを実装する型は、異なるサイズの型であるかもしれません。この" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"場合、型が、上の例にあるような `Vec<Pet>`のようなものを持つことは不可能となり" | ||
"ます。" | ||
|
||
#: src/traits/trait-objects.md:70 | ||
msgid "" | ||
"`dyn Pet` is a way to tell the compiler about a dynamically sized type that " | ||
"implements `Pet`." | ||
msgstr "" | ||
"`dyn Pet` はコンパイラに、動的なサイズを持ち、かつ`Pet`を実装する型について知" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"らせるための方法です。" | ||
|
||
#: src/traits/trait-objects.md:72 | ||
msgid "" | ||
"In the example, `pets` is allocated on the stack and the vector data is on " | ||
"the heap. The two vector elements are _fat pointers_:" | ||
msgstr "" | ||
"上の例では `pets` はスタックに確保され、vectorのデータはヒープ上にあります。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"二つのベクターの要素は _ファット_ ポインタです:" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/trait-objects.md:74 | ||
msgid "" | ||
|
@@ -8169,16 +8237,22 @@ msgid "" | |
"wikipedia.org/wiki/Virtual_method_table) (vtable) for the `Pet` " | ||
"implementation of that particular object." | ||
msgstr "" | ||
"ファットポインタはdouble-widthポインタです。これは二つの要素からなります:実" | ||
"際のオブジェクトへのポインタと、そのオブジェクトの`Pet`の実装に向けた[仮想メ" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"ソッドテーブル](https://en.wikipedia.org/wiki/Virtual_method_table) (vtable)" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"です。" | ||
|
||
#: src/traits/trait-objects.md:77 | ||
msgid "" | ||
"The data for the `Dog` named Fido is the `name` and `age` fields. The `Cat` " | ||
"has a `lives` field." | ||
msgstr "" | ||
"Fidoと名付けられた`Dog`のデータは`name` と `age` のフィールドに対応します。例" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"の`Cat`には`lives` フィールドがあります。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/trait-objects.md:79 | ||
msgid "Compare these outputs in the above example:" | ||
msgstr "" | ||
msgstr "上の例の結果を比べてみましょう:" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/trait-objects.md:80 | ||
msgid "" | ||
|
@@ -8197,10 +8271,13 @@ msgid "" | |
"Rust derive macros work by automatically generating code that implements the " | ||
"specified traits for a data structure." | ||
msgstr "" | ||
"Rustのderiveマクロは、データ構造に対して、指定されたトレイトを実装するコード" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"を自動的に生成します。" | ||
|
||
#: src/traits/deriving-traits.md:5 | ||
msgid "You can let the compiler derive a number of traits as follows:" | ||
msgstr "" | ||
"コンパイラには、以下のような幾つかのトレイトを導出させることができます:" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/deriving-traits.md:7 | ||
msgid "" | ||
|
@@ -8224,6 +8301,8 @@ msgstr "" | |
#: src/traits/default-methods.md:3 | ||
msgid "Traits can implement behavior in terms of other trait methods:" | ||
msgstr "" | ||
"トレイトを用いて、異なるトレイトメソッドに関した動作を実装することができま" | ||
"す:" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/default-methods.md:5 | ||
msgid "" | ||
|
@@ -8259,34 +8338,42 @@ msgid "" | |
"are required to implement themselves. Methods with default implementations " | ||
"can rely on required methods." | ||
msgstr "" | ||
"トレイトは予め実装された(デフォルトの)メソッドと、ユーザが自身で実装する必" | ||
"要のあるメソッドを指定することがあります。デフォルトの実装のあるメソッドは、" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"必須のメソッドのみに依存します。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/default-methods.md:35 | ||
msgid "Move method `not_equals` to a new trait `NotEquals`." | ||
msgstr "" | ||
"メソッド `not_equals` を新しいトレイト `NotEquals` に移してみましょう。" | ||
|
||
#: src/traits/default-methods.md:37 | ||
msgid "Make `Equals` a super trait for `NotEquals`." | ||
msgstr "" | ||
msgstr "`Equals` を `NotEquals` のスーパートレイトにしてみましょう。" | ||
|
||
#: src/traits/default-methods.md:46 | ||
msgid "Provide a blanket implementation of `NotEquals` for `Equals`." | ||
msgstr "" | ||
msgstr "`Equals`に対する`NotEquals`のブランケット実装を示してください。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/default-methods.md:58 | ||
msgid "" | ||
"With the blanket implementation, you no longer need `Equals` as a super " | ||
"trait for `NotEqual`." | ||
msgstr "" | ||
"ブランケット実装を用いれば、`NotEqual` のスーパートレイトとしての`Equals` は" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"もう必要ありません。" | ||
|
||
#: src/traits/trait-bounds.md:3 | ||
msgid "" | ||
"When working with generics, you often want to require the types to implement " | ||
"some trait, so that you can call this trait's methods." | ||
msgstr "" | ||
"ジェネリクスを用いるとき、トレイトのメソッドを呼び出せるように、型にいくつか" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"のトレイトを実装することを必須にしたいことがよくあります。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/trait-bounds.md:6 | ||
msgid "You can do this with `T: Trait` or `impl Trait`:" | ||
msgstr "" | ||
msgstr "そうしたことは`T: Trait` や `impl Trait`を用いて行えます:" | ||
|
||
#: src/traits/trait-bounds.md:8 | ||
msgid "" | ||
|
@@ -8315,24 +8402,54 @@ msgid "" | |
"}\n" | ||
"```" | ||
msgstr "" | ||
"```rust,editable\n" | ||
"fn duplicate<T: Clone>(a: T) -> (T, T) {\n" | ||
" (a.clone(), a.clone())\n" | ||
"}\n" | ||
"\n" | ||
"// 以下の糖衣構文です:\n" | ||
"// fn add_42_millions<T: Into<i32>>(x: T) -> i32 {\n" | ||
"fn add_42_millions(x: impl Into<i32>) -> i32 {\n" | ||
" x.into() + 42_000_000\n" | ||
"}\n" | ||
"\n" | ||
"// struct NotClonable;\n" | ||
"\n" | ||
"fn main() {\n" | ||
" let foo = String::from(\"foo\");\n" | ||
" let pair = duplicate(foo);\n" | ||
" println!(\"{pair:?}\");\n" | ||
"\n" | ||
" let many = add_42_millions(42_i8);\n" | ||
" println!(\"{many}\");\n" | ||
" let many_more = add_42_millions(10_000_000);\n" | ||
" println!(\"{many_more}\");\n" | ||
"}\n" | ||
"```" | ||
|
||
#: src/traits/trait-bounds.md:35 | ||
msgid "Show a `where` clause, students will encounter it when reading code." | ||
msgstr "" | ||
"`where` 節を見せてみましょう。受講生はコードを読んでいるときに、この`where`節" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"に遭遇します。" | ||
|
||
#: src/traits/trait-bounds.md:46 | ||
msgid "It declutters the function signature if you have many parameters." | ||
msgstr "" | ||
"たくさんのパラメタがある場合に、`where`節は関数のシグネチャを整理整頓してくれ" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たくさんのパラメタがある場合、関数シグネチャが煩雑になります。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I would prefer describing positive outcome of using |
||
"ます。" | ||
|
||
#: src/traits/trait-bounds.md:47 | ||
msgid "It has additional features making it more powerful." | ||
msgstr "" | ||
msgstr "`where`節をさらに強力にするような追加の特徴があります。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/trait-bounds.md:48 | ||
msgid "" | ||
"If someone asks, the extra feature is that the type on the left of \":\" can " | ||
"be arbitrary, like `Option<T>`." | ||
msgstr "" | ||
"もし誰かに尋ねられたとき、追加の特徴は、「:」の左の型は任意であるというもので" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"す。`Option<T>`のように。" | ||
|
||
#: src/traits/impl-trait.md:1 | ||
msgid "`impl Trait`" | ||
|
@@ -8343,6 +8460,8 @@ msgid "" | |
"Similar to trait bounds, an `impl Trait` syntax can be used in function " | ||
"arguments and return values:" | ||
msgstr "" | ||
"トレイト境界と似たように、構文 `impl Trait`は関数の引数と返り値においてのみ利" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"用可能です:" | ||
|
||
#: src/traits/impl-trait.md:6 | ||
msgid "" | ||
|
@@ -8363,24 +8482,31 @@ msgstr "" | |
#: src/traits/impl-trait.md:19 | ||
msgid "`impl Trait` allows you to work with types which you cannot name." | ||
msgstr "" | ||
"`impl Trait`を用いれば、名前を付けることができない型を活用することができま" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"す。" | ||
|
||
#: src/traits/impl-trait.md:23 | ||
msgid "" | ||
"The meaning of `impl Trait` is a bit different in the different positions." | ||
msgstr "" | ||
msgstr "`impl Trait`の意味は、位置によって少し異なります。" | ||
|
||
#: src/traits/impl-trait.md:25 | ||
msgid "" | ||
"For a parameter, `impl Trait` is like an anonymous generic parameter with a " | ||
"trait bound." | ||
msgstr "" | ||
"パラメタに対しては、`impl Trait`は、トレイト境界を持つ匿名のジェネリックパラ" | ||
"メタのようなものです。" | ||
|
||
#: src/traits/impl-trait.md:27 | ||
msgid "" | ||
"For a return type, it means that the return type is some concrete type that " | ||
"implements the trait, without naming the type. This can be useful when you " | ||
"don't want to expose the concrete type in a public API." | ||
msgstr "" | ||
"返り値に対しては、トレイトを実装する具象型と同じ返り値の型をとり、かつ型に名" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"前をつけることはない、ということです。このことは公開されるAPIに具象型を晒した" | ||
"くない場合に便利です。" | ||
|
||
#: src/traits/impl-trait.md:31 | ||
msgid "" | ||
|
@@ -8391,6 +8517,12 @@ msgid "" | |
"`let x: Vec<_> = foo.collect()` or with the turbofish, `foo.collect::" | ||
"<Vec<_>>()`." | ||
msgstr "" | ||
"返り値の位置における型推論は困難です。`impl Foo`を返す関数は、それが返す具象" | ||
"型はソースコードに書かれることないまま、具象型を選びます。`collect<B>() -> B`" | ||
"のようなジェネリック型を返す関数は、`B`を満たすどのような型でも返すことがあり" | ||
"ます。 また、関数の呼び出し元はそのような型を一つを選ぶ必要があるかもしれませ" | ||
"ん。 それは、 `let x: Vec<_> = foo.collect()`としたり、turbofishを用いて`foo." | ||
"collect::<Vec<_>>()`とすることで行えます。" | ||
|
||
#: src/traits/impl-trait.md:37 | ||
msgid "" | ||
|
@@ -8402,6 +8534,13 @@ msgid "" | |
"`format!` returns. If we wanted to do the same via `: Display` syntax, we'd " | ||
"need two independent generic parameters." | ||
msgstr "" | ||
"この例は素晴らしい例です。なぜなら、 `impl Display`を2回用いているからで" | ||
"す。 ここでは `impl Display` の型が同一になることを強制するものはない、という" | ||
"説明をするのに役立ちます。もし単一の`T: Display`を用いた場合、定数に対して入" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"力の`T`と返り値の`T`が同一の型であることが強制されてしまいます。例で示した関" | ||
"数ではうまくいかないでしょう。なぜなら、我々が期待する入力の型は、`format!`が" | ||
"返すものではおそらくないからです。もしも同じことを`: Display`の構文で行いたい" | ||
"場合、2つの独立してジェネリックなパラメタが必要となるでしょう。" | ||
kantasv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: src/traits/important-traits.md:3 | ||
msgid "" | ||
|
Uh oh!
There was an error while loading. Please reload this page.