- 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.
推薦閱讀
- Go Web編程
- Android項目開發入門教程
- OpenShift開發指南(原書第2版)
- Raspberry Pi for Secret Agents(Third Edition)
- Java:Data Science Made Easy
- Modular Programming in Java 9
- Node Cookbook(Second Edition)
- Windows Embedded CE 6.0程序設計實戰
- Penetration Testing with the Bash shell
- Machine Learning for OpenCV
- Building Business Websites with Squarespace 7(Second Edition)
- Learning Unreal Engine Game Development
- Python編程入門(第3版)
- 優化驅動的設計方法
- MySQL核心技術與最佳實踐