Merge pull request #284 from mg-chao/20220122_translate_traits

[rust-exercise] 翻译 traits 部分
pull/287/head
Sunface 3 years ago committed by GitHub
commit 63e1a77c67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,19 +1,19 @@
# Traits # 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: 另外一些常见的 Rust trait 包括:
- `Clone` (the `clone` method) - `Clone` `clone` 方法)
- `Display` (which allows formatted display via `{}`) - `Display` (实现通过 `{}` 进行格式化显示)
- `Debug` (which allows formatted display via `{:?}`) - `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) - [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html)

@ -1,12 +1,10 @@
// traits1.rs // traits1.rs
// Time to implement some traits! // 是时候来实现些 trait 了!
// //
// Your task is to implement the trait // 你的任务是为 `String` 实现 `AppendBar` trait。
// `AppendBar' for the type `String'.
// //
// The trait AppendBar has only one function, // `AppendBar` 只有一个函数,它将 "Bar" 追加到任何实现该 trait 的对象上。
// which appends "Bar" to any object // 译Append 有追加、附加的意思,所以“追加/附加 Bar”。
// implementing this trait.
// I AM NOT DONE // I AM NOT DONE
@ -15,7 +13,7 @@ trait AppendBar {
} }
impl AppendBar for String { impl AppendBar for String {
//Add your code here // 在这里编写代码
} }
fn main() { fn main() {

@ -1,14 +1,10 @@
// traits2.rs // traits2.rs
// //
// Your task is to implement the trait // 你的任务是为一个字符串 vector 实现 `AppendBar` trait。
// `AppendBar' for a vector of strings.
// //
// To implement this trait, consider for // 为了实现该 trait请思考下将 "Bar" 追加到字符串 vector 的意义是什么?
// a moment what it means to 'append "Bar"'
// to a vector of strings.
// //
// No boiler plate code this time, // 这次没有样板代码,相信自己!
// you can do this!
// I AM NOT DONE // I AM NOT DONE
@ -16,7 +12,7 @@ trait AppendBar {
fn append_bar(self) -> Self; fn append_bar(self) -> Self;
} }
//TODO: Add your code here //TODO:在这编写代码
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

@ -590,7 +590,7 @@ name = "traits1"
path = "exercises/traits/traits1.rs" path = "exercises/traits/traits1.rs"
mode = "test" mode = "test"
hint = """ hint = """
A discussion about Traits in Rust can be found at: Rust Traits
https://doc.rust-lang.org/book/ch10-02-traits.html https://doc.rust-lang.org/book/ch10-02-traits.html
""" """
@ -599,11 +599,11 @@ name = "traits2"
path = "exercises/traits/traits2.rs" path = "exercises/traits/traits2.rs"
mode = "test" mode = "test"
hint = """ hint = """
Notice how the trait takes ownership of 'self',and returns `Self'. trait 'self' 'Self'
Try mutating the incoming string vector. vector
Vectors provide suitable methods for adding an element at the end. See Vector
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html""" https://doc.rust-lang.org/std/vec/struct.Vec.html"""
# TESTS # TESTS

Loading…
Cancel
Save