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

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.

主站蜘蛛池模板: 虞城县| 藁城市| 左贡县| 横峰县| 丰镇市| 叙永县| 荆州市| 佛坪县| 商丘市| 黄大仙区| 常熟市| 佛冈县| 阳谷县| 黄浦区| 攀枝花市| 新巴尔虎左旗| 宣武区| 婺源县| 乾安县| 甘洛县| 治多县| 江源县| 福鼎市| 滁州市| 济南市| 百色市| 屏东县| 灵石县| 汾阳市| 洛南县| 南京市| 涟源市| 潼关县| 工布江达县| 当雄县| 广东省| 高青县| 锦州市| 清水县| 元阳县| 塘沽区|