| Description of Generics |
|
Description
Generics are a facility to write code for multiple contexts with different typesin Rust generics refer to the parameterization of data types and traits generics provide more concise and clean code by reducing code duplication and providing type-safety concept of generics can be applied to methods, functions, structures, enumerations, collections and traits The <T> syntax known as the type parameter is used to declare a generic construct T represents any data-type Generic Collection
following example declares a vector that can store only integers
fn main(){
let mut vector_integer: Vec
below example shows that a vector of integer type can only store integer valuespushing a string value into the collection will cause the compiler to return an error type safety fn main() {
let mut vector_integer: Vec
Generic Structure
the type parameter represents a type, which the compiler will fill in laterexample declares a generic structure named Data the <T> type indicates some data type the main() function creates two instances of the structure. struct Data<T> {
value:T,
}
fn main() {
//generic type of i32
let t:Data<i32> = Data{value:350};
println!("value is :{} ",t.value);
//generic type of String
let t2:Data<String> = Data{value:"Tom".to_string()};
println!("value is :{} ",t2.value);
}
output
value is :350 value is :Tom |
| Traits |
|
Definition
traits can be used to implement a standard set of behaviors (methods) across multiple structurestraits are like interfaces in OOP Declare a Trait
Traits can contain concrete methods (methods with body) or abstract methods (methods without a body)use a concrete method if the method definition will be shared by all structures implementing the trait a structure can choose to override a function defined by the trait use abstract methods if the method definition varies for the implementing structures trait some_trait {
// abstract or method which is empty
fn method1(&self);
// this is already implemented, this is free
fn method2(&self){
//some contents of method2
}
}
Implement a Trait
syntax
impl some_trait for structure_name {
// implement method1()
fn method1(&self ){
}
}
examples defines a trait Printable with a method print() which is implemented by the structure book
fn main(){
// create an instance of the structure
let b1 = Book {
id:1001,
name:"Rust in Action"
};
b1.print();
}
// declare a structure
struct Book {
name:&'static str,
id:u32
}
// declare a trait
trait Printable {
fn print(&self);
}
//implement the trait
impl Printable for Book {
fn print(&self){
println!("Printing book with id:{} and name {}",self.id,self.name)
}
}
output
Printing book with id:1001 and name Rust in Action Note : not from tutorial a struct can have multiple impl blocks trait ThisWay {
fn apply(&self) -> Vec
|
| Generic Functions |
|
example defines a generic function that displays a parameter passed to it. The parameter can be of any type the parameter's type should implement the Display trait so that its value can be printed by the println! macro use std::fmt::Display;
fn main(){
print_pro(10 as u8);
print_pro(20 as u16);
print_pro("Hello TutorialsPoint");
}
fn print_pro<T:Display>(t:T){
println!("Inside print_pro generic function:");
println!("{}",t);
}
output
Inside print_pro generic function: 10 Inside print_pro generic function: 20 Inside print_pro generic function: Hello TutorialsPoint |