2024-06-18 13:05:28 +08:00

20 lines
755 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 特徵 (Traits)
特徵是一組方法的集合。
資料類型可以實現特徵。為此,需要為資料類型定義構成特徵的方法。例如,`String` 資料類型實現了 `From<&str>` 特徵。這使得用戶可以編寫 `String::from("hello")`
這樣,特徵在某種程度上類似於 Java 的接口interfaces和 C++ 的抽象類abstract classes
一些其他常見的 Rust 特徵包括:
- `Clone``clone` 方法)
- `Display`(允許透過 `{}` 進行格式化顯示)
- `Debug`(允許透過 `{:?}` 進行格式化顯示)
由於特徵定義了資料類型之間的共享行為,它們在編寫泛型時非常有用。
## 進一步了解
- [特徵](https://doc.rust-lang.org/book/ch10-02-traits.html)