This commit is contained in:
TimLai666 2024-06-18 13:05:28 +08:00 committed by GitHub
parent a3043adc54
commit c40b3fc7aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 27 deletions

View File

@ -1,11 +1,8 @@
# Generics
# 泛型
Generics is the topic of generalizing types and functionalities to broader cases.
This is extremely useful for reducing code duplication in many ways, but can call for rather involving syntax.
Namely, being generic requires taking great care to specify over which types a generic type is actually considered valid.
The simplest and most common use of generics is for type parameters.
泛型是一個將類型和功能擴展到更廣泛情況的主題。這在許多方面對減少代碼重複非常有用,但也可能需要相當複雜的語法。具體來說,泛型需要非常謹慎地指定泛型類型實際上在哪些類型上是有效的。最簡單和最常見的泛型使用方式是類型參數。
## Further information
## 進一步了解
- [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html)
- [Bounds](https://doc.rust-lang.org/rust-by-example/generics/bounds.html)
- [泛型資料類型](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html)
- [邊界](https://doc.rust-lang.org/rust-by-example/generics/bounds.html)

View File

@ -1,10 +1,8 @@
// generics1.rs
//
// This shopping list program isn't compiling! Use your knowledge of generics to
// fix it.
// 這個購物清單程式無法編譯!用您對泛型的了解來修復它。
//
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint generics1` 或使用 `hint` watch 子命令來獲取提示。
// I AM NOT DONE

View File

@ -1,10 +1,9 @@
// generics2.rs
//
// This powerful wrapper provides the ability to store a positive integer value.
// Rewrite it using generics so that it supports wrapping ANY type.
// 這個強大的包裝器提供了存儲正整數值的能力。
// 使用泛型重寫它,使其支援包裝任何類型。
//
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint generics2` 或使用 `hint` watch 子命令來獲取提示。
// I AM NOT DONE

View File

@ -1,19 +1,19 @@
# Traits
# 特徵 (Traits)
A trait is a collection of methods.
特徵是一組方法的集合。
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")`.
資料類型可以實現特徵。為此,需要為資料類型定義構成特徵的方法。例如,`String` 資料類型實現了 `From<&str>` 特徵。這使得用戶可以編寫 `String::from("hello")`
In this way, traits are somewhat similar to Java interfaces and C++ abstract classes.
這樣,特徵在某種程度上類似於 Java 的接口interfaces和 C++ 的抽象類abstract classes
Some additional common Rust traits include:
一些其他常見的 Rust 特徵包括:
- `Clone` (the `clone` method)
- `Display` (which allows formatted display via `{}`)
- `Debug` (which allows formatted display via `{:?}`)
- `Clone``clone` 方法)
- `Display`(允許透過 `{}` 進行格式化顯示)
- `Debug`(允許透過 `{:?}` 進行格式化顯示)
Because traits indicate shared behavior between data types, they are useful when writing generics.
由於特徵定義了資料類型之間的共享行為,它們在編寫泛型時非常有用。
## Further information
## 進一步了解
- [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html)
- [特徵](https://doc.rust-lang.org/book/ch10-02-traits.html)