官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 留坝县| 广元市| 镇赉县| 霍城县| 昌黎县| 陇南市| 侯马市| 武强县| 廊坊市| 吴旗县| 许昌县| 天等县| 甘南县| 鲜城| 泾阳县| 于都县| 靖宇县| 新邵县| 嘉兴市| 珠海市| 滨海县| 延庆县| 宣城市| 长武县| 商都县| 平江县| 纳雍县| 沧源| 遂川县| 桑植县| 贡嘎县| 察哈| 静宁县| 林西县| 桐乡市| 安丘市| 仁化县| 汉川市| 色达县| 永宁县| 体育|