Rust - Generics Last Updated : 08 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Generics in Rust is a method of generalizing data types. Generics are always defined at run time. As generics can have multiple forms over given parameters and can be applied to methods, functions, structures, traits, etc they are often referred to as parametric polymorphism in type theory. Generics allow writing clean and concise code thereby providing type safety and refactored code. In Generics, we have <T> as the type parameter which is used to declare a generic construct T. T can be of any data type. Now we see an example for generic collections, we create a vector that has an integer of i32 type. Example 1: Rust fn main(){ let mut _int_vector: Vec<i32> = vec![10,20]; // initializing integer vectors _int_vector.push(30); // integer number is pushed to a vector println!("{:?}",_int_vector); } Output: The <T> type indicates the generic type specifier for the Data. Example 2: Rust // Generic Struct in RUST struct Data<T> { value:T, } fn main() { // generic type of String let type1:Data<String> = Data{value:"GFG".to_string()}; println!("Organization :{} ",type1.value); // generic type of i32 let type2:Data<i32> = Data{value:2022}; println!("Year :{} ",type2.value); } Output: Here, the merge function takes a value of the string as well as takes a value of i32. T is the generic type specifier. Example 3: Rust // Generic Functions in RUST fn main(){ println!("****** Passing String literals*******"); merge("Geeks for ", " Geeks "); println!("******** Passing an integer ***********"); merge(20 as i32, 22 as i32); } use std::fmt::Display; fn merge<T:Display>(t:T, s:T){ let result = format!("{}{}", t , s); println!("{}", result); } Output: Comment More infoAdvertise with us Next Article Rust - Generics S sayanc170 Follow Improve Article Tags : Rust Rust generics Similar Reads Rust - Generic Traits Rust is an extremely fast programming language that supports speed, concurrency, as well as multithreading. In Rust, we have a concept of Generic Traits where Generics are basically an abstraction of various properties, and Traits are basically used for combining generic types for constraining types 2 min read Rust - Generic Function In Rust, Generic functions are very useful. Generic make code more flexible and provide more functionality to the callers of the function. It prevents code duplication as it is not required to define different functions of different types. Generics are specified in the signature of function where we 3 min read Kotlin generics Generics are one of Kotlin's most powerful features. They allow us to write flexible, reusable, and type-safe code. With generics, we can define classes, methods, and properties that work with different types while still maintaining compile-time type safety.What Are Generics?A generic type is a clas 6 min read Rust - Interface Rust is a systems programming language with a concept of Interfaces in Rust that is implemented by Traits.Trait in Rust is basically a struct having no bodies for the methods. The logic is generally not defined but only the methods are. Rust enforces us to follow a set of coding practices with imple 3 min read Rust - Enum An enum in Rust is a custom data type that represents data that can be anyone among several possible variants. Each variant in the enum can optionally have data associated with it. An enumerated type is defined using the enum keyword before the name of the enumeration. It also consists of methods. S 3 min read Like