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

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.

主站蜘蛛池模板: 合川市| 克拉玛依市| 武安市| 静安区| 满洲里市| 安庆市| 阳高县| 西昌市| 湄潭县| 瑞昌市| 仁怀市| 溧水县| 营山县| 东山县| 巍山| 望城县| 兖州市| 沽源县| 茌平县| 木里| 丰城市| 崇阳县| 平武县| 卢龙县| 讷河市| 安陆市| 三门峡市| 乳源| 正宁县| 涿州市| 怀远县| 建昌县| 临朐县| 尉氏县| 渝北区| 美姑县| 双峰县| 宝兴县| 阳谷县| 淮北市| 屯门区|