- 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.
推薦閱讀
- 數據庫系統原理及MySQL應用教程(第2版)
- Learn TypeScript 3 by Building Web Applications
- Spring 5企業級開發實戰
- Reporting with Visual Studio and Crystal Reports
- C# 2012程序設計實踐教程 (清華電腦學堂)
- R語言編程指南
- Full-Stack Vue.js 2 and Laravel 5
- Web程序設計(第二版)
- Learning Concurrency in Kotlin
- Mastering React
- Python從入門到精通
- Flowable流程引擎實戰
- R數據科學實戰:工具詳解與案例分析
- Fastdata Processing with Spark
- Anaconda數據科學實戰