- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 151字
- 2021-07-02 23:07:18
Generics and arrays
For those coming from a C# or C++ background, you will no doubt be used to generic types (often referred to as having a type T); you will be used to seeing things like this:
T a = new T();
Generics allow defining methods for several types. In its most general form, T means "any type." The following function, for instance, takes two arguments that can be any type T:
fn generic_function<T>(a: T, b: T)
T, as has been established, can be of any type. This means that we cannot do much with them, since only a few methods are implemented for "any type." For instance, if we would like to add these variables together, we would need to restrict the generic types somewhat. We would essentially need to tell Rust that "T may be of any type, as long as it implements addition." More about this later.
推薦閱讀
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- 案例式C語言程序設計
- 零基礎學C++程序設計
- Oracle 11g從入門到精通(第2版) (軟件開發視頻大講堂)
- Apache Hive Essentials
- Mastering LibGDX Game Development
- 單片機應用技術
- C語言程序設計
- 3D少兒游戲編程(原書第2版)
- Learning ArcGIS for Desktop
- Mastering Xamarin.Forms(Second Edition)
- 編寫高質量代碼:改善Objective-C程序的61個建議
- INSTANT Silverlight 5 Animation
- OpenCV with Python Blueprints
- Hack與HHVM權威指南