From e9c7e18fad8bb56234b92f45f457477bc8430696 Mon Sep 17 00:00:00 2001 From: mg-chao Date: Sat, 22 Jan 2022 20:54:36 +0800 Subject: [PATCH] update: translate traits --- exercise/exercises/traits/README.md | 20 ++++++++++---------- exercise/exercises/traits/traits1.rs | 12 +++++------- exercise/exercises/traits/traits2.rs | 12 ++++-------- exercise/info.toml | 10 +++++----- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/exercise/exercises/traits/README.md b/exercise/exercises/traits/README.md index de67acd0..09162ffc 100644 --- a/exercise/exercises/traits/README.md +++ b/exercise/exercises/traits/README.md @@ -1,19 +1,19 @@ # Traits -A trait is a collection of methods. +Trait 是一系列方法的集合。 -Data types can implement traits. To do so, the methods making up the trait are defined for the data type. For example, the `String` data type implements the `From<&str>` trait. This allows a user to write `String::from("hello")`. +数据类型可以实现 trait。为此需要帮数据类型定义好构成 trait 的方法。 +例如,`String` 类型实现了 `From<&str>` trait。它赋予我们能力写出 `String::from("hello")`。 -In this way, traits are somewhat similar to Java interfaces and C++ abstract classes. +如此一来,trait 就有点类似于 Java 的接口和 C++ 的抽象类。 -Some additional common Rust traits include: -- `Clone` (the `clone` method) -- `Display` (which allows formatted display via `{}`) -- `Debug` (which allows formatted display via `{:?}`) +另外一些常见的 Rust trait 包括: +- `Clone` (`clone` 方法) +- `Display` (实现通过 `{}` 进行格式化显示) +- `Debug` (实现通过 `{:?}` 进行格式化显示 ) -Because traits indicate shared behavior between data types, they are useful when writing generics. +因为 trait 标明了数据类型之间的共有行为,所以它在编写泛型时非常有用。 - -## Further information +## 更多信息 - [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html) diff --git a/exercise/exercises/traits/traits1.rs b/exercise/exercises/traits/traits1.rs index 15e08f24..1d8b5d84 100644 --- a/exercise/exercises/traits/traits1.rs +++ b/exercise/exercises/traits/traits1.rs @@ -1,12 +1,10 @@ // traits1.rs -// Time to implement some traits! +// 是时候来实现些 trait 了! // -// Your task is to implement the trait -// `AppendBar' for the type `String'. +// 你的任务是为 `String` 实现 `AppendBar` trait。 // -// The trait AppendBar has only one function, -// which appends "Bar" to any object -// implementing this trait. +// `AppendBar` 只有一个函数,它将 "Bar" 追加到任何实现该 trait 的对象上。 +// 译:Append 有追加、附加的意思,所以“追加/附加 Bar”。 // I AM NOT DONE @@ -15,7 +13,7 @@ trait AppendBar { } impl AppendBar for String { - //Add your code here + // 在这里编写代码 } fn main() { diff --git a/exercise/exercises/traits/traits2.rs b/exercise/exercises/traits/traits2.rs index 916c3c4b..a8520cca 100644 --- a/exercise/exercises/traits/traits2.rs +++ b/exercise/exercises/traits/traits2.rs @@ -1,14 +1,10 @@ // traits2.rs // -// Your task is to implement the trait -// `AppendBar' for a vector of strings. +// 你的任务是为一个字符串 vector 实现 `AppendBar` trait。 // -// To implement this trait, consider for -// a moment what it means to 'append "Bar"' -// to a vector of strings. +// 为了实现该 trait,请思考下将 "Bar" 追加到字符串 vector 的意义是什么? // -// No boiler plate code this time, -// you can do this! +// 这次没有样板代码,相信自己! // I AM NOT DONE @@ -16,7 +12,7 @@ trait AppendBar { fn append_bar(self) -> Self; } -//TODO: Add your code here +//TODO:在这编写代码 #[cfg(test)] mod tests { diff --git a/exercise/info.toml b/exercise/info.toml index 87e9d830..aa5e3474 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -590,7 +590,7 @@ name = "traits1" path = "exercises/traits/traits1.rs" mode = "test" hint = """ -A discussion about Traits in Rust can be found at: +下面的网站是关于 Rust 中 Traits 的讨论: https://doc.rust-lang.org/book/ch10-02-traits.html """ @@ -599,11 +599,11 @@ name = "traits2" path = "exercises/traits/traits2.rs" mode = "test" hint = """ -Notice how the trait takes ownership of 'self',and returns `Self'. -Try mutating the incoming string vector. +注意 trait 是如何取得 'self' 的所有权,并返回一个 'Self' 的。 +尝试修改传入的字符串 vector。 -Vectors provide suitable methods for adding an element at the end. See -the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html""" +Vector 已提供了在它尾部添加一个元素的方法。 +参阅文档:https://doc.rust-lang.org/std/vec/struct.Vec.html""" # TESTS