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

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.

主站蜘蛛池模板: 长葛市| 诏安县| 晋宁县| 荣昌县| 马公市| 南昌县| 灵山县| 阜新| 子长县| 黎川县| 和龙市| 舒城县| 张家港市| 清流县| 白城市| 沂南县| 闽清县| 芦溪县| 黑龙江省| 湘西| 龙胜| 长泰县| 迭部县| 青州市| 依安县| 襄樊市| 七台河市| 平顶山市| 沙雅县| 西充县| 平和县| 花垣县| 阿拉善右旗| 依安县| 宜丰县| 广元市| 敖汉旗| 卢氏县| 淮南市| 南澳县| 喜德县|