- 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.
推薦閱讀
- Data Visualization with D3 4.x Cookbook(Second Edition)
- iOS Game Programming Cookbook
- SOA實踐
- 神經(jīng)網(wǎng)絡編程實戰(zhàn):Java語言實現(xiàn)(原書第2版)
- INSTANT Sencha Touch
- Scratch 3.0少兒編程與邏輯思維訓練
- 區(qū)塊鏈技術(shù)與應用
- Machine Learning With Go
- .NET 4.5 Parallel Extensions Cookbook
- 體驗之道:從需求到實踐的用戶體驗實戰(zhàn)
- Django 2.0 入門與實踐
- 構(gòu)建跨平臺APP:響應式UI設計入門
- Cadence Concept-HDL & Allegro原理圖與電路板設計(第2版)
- WebGIS之Leaflet全面解析
- Flask Framework Cookbook