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

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.

主站蜘蛛池模板: 万年县| 日土县| 安新县| 上饶县| 明溪县| 郎溪县| 吉林市| 阿巴嘎旗| 四平市| 鲁甸县| 隆安县| 南通市| 新闻| 濮阳县| 浮山县| 疏勒县| 荔浦县| 富阳市| 邵阳市| 五寨县| 临朐县| 股票| 磐安县| 来宾市| 怀柔区| 五莲县| 白银市| 库伦旗| 遂川县| 宾阳县| 陆丰市| 墨竹工卡县| 海阳市| 新绛县| 寻乌县| 福清市| 辽阳市| 抚顺市| 双城市| 游戏| 镇宁|