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
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)

@ -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() {

@ -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 {

@ -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

Loading…
Cancel
Save