Monday, February 18, 2019

Trait in rust

trait rust

Trait in Rust programming

I believe people those who are from object oriented programming languages understand the usage of Trait easily . I have seen people saying Trait are similar to Interfaces in Java.
But we will start with zero perquisites or background knowledge on those. Only prerequisite would be to basic understanding on how “Struct” or user defined data types are created in Rust.

Trait - Collection of methods defined for unknown types.

The definition for Trait is an abstract over behavior that types can have in common.
don’t worry , if we are not able to digest the definition fully.
You might have noticed that in the real world lot of objects shares same behavior and they give the output to the behavior differently.
eg: Car and Bus can provide “ride”
Cat and Dog can have “talk” method
in a Game different objects can have “bonus”
We know these methods/functions are needed for these objects ,but they should produce different output for them differently for different types of objects.
eg : when Cat call “talk” method output would be “Meow Meow”
when Dog call talk method output would be “woof woof”
Also we don’t want other types of objects calling the method if they are not implemented
eg: in my case Cow can’t talk
It would be great if I get noticed in the compile time itself that there is no implementation for Cow for “talk” . rather than future at some point of time in the future the code goes through that piece code and fails badly.
So we can identify the shared behavior of objects in the design phase of your project, the ideal way of defining these common behavior will be through “Trait”
Now you have understood the use case of Trait .
I think we can jump into an example
example creates 3 structs for Cat, Dog and Cow
/*We create/define a trait ( unknown type) using keyword "trait" called Speaker and define the behavior of the trait.
in this example trait accept the unknown type which implements the trait and returns nothing.
Trait name is Speaker and method defined is speak. So we are going to call this "speak" method for  the struct object implements this.
*/

pub trait Speaker {
     fn speak(&self) -> ();
 }


/*Next step would be implementing the trait for whichever types have the common  behavior.
This case our Cat and Dog speaks. 
so implemented them.*/

impl Speaker for Cat { 
 fn speak(&self) { println!("meow!"); }
 }

/* Now we can call them from "main" function as it is in scope.First we need to create struct object and then call the trait defined method (speak) with dot(.) operator. */

cat1.speak();


pub struct Cat{
    color:(u8,u8,u8),
    
}
pub struct Dog{
    color:(u8,u8,u8),
}

pub struct Cow{
    color:(u8,u8,u8),
}


pub trait Speaker {
     fn speak(&self) -> ();
 }



 impl Speaker for Cat {
     fn speak(&self) {
           println!("meow!");
     }
 }

 impl Speaker for Dog {
     fn speak(&self) {
           println!("woof!");
     }
 }
 
fn main(){
    let cat1 = Cat{color:(30,30,30)};
    let dog1 = Dog{color:(30,30,30)};
    let cow1 = Cow{color:(30,30,30)};
    cat1.speak();
    dog1.speak();
    //cow1.speak(); fails in compile time saying "no implementaton of the trait"
}

Trait Bounds
Trait bounds restricts the function to have the types which implements the trait.So the function can not be called with any other types.Code that calls the function with any other type, like a String or an i32, won’t compile, because those types don’t implement "Speaker"

pub struct Cat{
    color:(u8,u8,u8),
    
}
pub struct Dog{
    color:(u8,u8,u8),
}

pub struct Cow{
    color:(u8,u8,u8),
}


pub trait Speaker {
     fn speak(&self) -> ();
 }



 impl Speaker for Cat {
     fn speak(&self) {
           println!("meow!");
     }
 }

 impl Speaker for Dog {
     fn speak(&self) {
           println!("woof!");
     }
 }

pub  fn notify(speaker: S) {
        speaker.speak();
}

fn main(){
    let cat1 = Cat{color:(30,30,30)};
    let dog1 = Dog{color:(30,30,30)};
    let cow1 = Cow{color:(30,30,30)};
    notify(cat1); 
//    notify(cow1); uncomment this line to see the compile time error.
}

In Rust a trait must be in scope for you to be able to call its methods.
Reference
https://doc.rust-lang.org/book/ch10-02-traits.html

Saturday, February 9, 2019

Rust macros are Amazing!!!

rust macro

Macros in Rust programming

Macros are a powerful feature in Rust programming language. Since it is compiled to syntax tree rather than string pre-processing, it has advantages over other language’s macro.

When I started with Rust macro, it was taking me to a new programming style altogether. You can easily convert the reusable part of your program to macros. It is called meta programming .

Declarative Macros

we are going to discuss only this type macro creation in this post.

Macros will try to match pattern passed , if it succeed the match proceed with code defined in curly brackets.

How to start with Rust macro: 

macro_rules! macroname{
 () => { }
}
macro_rules! = is the macro using in rust for creating 
'macro_name' = is the name you want to be for the macro which can be invoked by *'macro_name!'*
( ) => {}

 () is the sytax for pattern matching 
 {} is the place we need to enter the code to exceuted.
 

($x:expr) 
$x is Name or variable
expr is type to match for name
  • ident: an identifier. Examples: x; foo.
  • path: a qualified name. Example: T::SpecialA.
  • expr: an expression. Examples: 2 + 2; if true { 1 } else { 2 }; f(42).
  • ty: a type. Examples: i32; Vec<(char, String)>; &T.
  • pat: a pattern. Examples: Some(t); (17, 'a'); _.
  • stmt: a single statement. Example: let x = 3.
  • block: a brace-delimited sequence of statements. Example: { log(error, "hi"); return 12; }.
  • item: an item. Examples: fn foo() { }; struct Bar;.
  • meta: a “meta item”, as found in attributes. Example: cfg(target_os = "windows").
  • tt: a single token tree.



Macro custom print

macro_rules! print_new{
//in this example we are passing the expression which has only one value which will be stored in $x
 
 //we need to define name and designator 
 // here **$x** is Name or variable
 // **expr** is type to match for name
    ($x:expr) => (
        println!("value={}",$x);
    )
}


fn main() {
    print_new!(5);
}

In rust almost everything is expression .In the above example we passed the ‘expr’ as 5 where 5 designated as name


example 2:
In this example we formatted the pattern as x=> 10+10 which equivalent to x=>$x:expr where $x get the value of 20
macro expression passing


example3:
Here we are passing function name as Identifer to generate/define a function
macro - building function

example4:

macro -apply function to a range of numbers


example5:

macro with pass dentifiers(variables)

Reference

https://words.steveklabnik.com/an-overview-of-macros-in-rust

https://danielkeep.github.io/tlborm/book/index.html

https://danielkeep.github.io/practical-intro-to-macros.html

Wednesday, February 6, 2019

Sum type with Enum in Rust programming language


Enum - Enumeration

Enumeration as user defined data type and in C programming language its mainly used for names to integral constants .

C syntax: 
Enum state { success = 1, failed = 0 };
Enums has very limited usage in C language. But Rust has used all use cases for Enum and adopted lot of use cases from Functional programming languages like Haskell and ML.
Rust Enum Syntax:
enums are the best example for Sum types. If you want to restrict the input to any one of the types which defined , then they are called some types.

Say you have 'Shape' Type, and you know that shape can be only Circle ,Square or Rectangle. You  want to do action for each types separately. then you can go with Enum.

sum type aslo called Tagged Union, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use.
The primary advantage of a tagged union/sum type over an untagged union is that all accesses are safe, and the compiler can even check that all cases are handled. 

Enum Shape{
        Circle(f32),
        Square(f32),
        Rectangle(f32,f32),
}





Enum example


Product type:

Struct is good example for product type. As we know, it is required to provide values for each types in a struct . It will be cartesian product of combinations we can make on struct types. It is must to provide value for each for defining an object. 




Importance of enum :

As I mentioned earlier, enum allows to define our own types which increase the readability
of the code as well as reduce the type issues in the programs.

For example , when we know we can only pass our defined type to function and if we can give a meaningful name for that type , which improve strength of the type system in our programs as well as improve the readability of the code.

if parent type consist of different child types, then it will be nice to use them with the hierarchy .Since it is not a product type, you can use one of them from parent at a time. For different type of input can do type validation and perform corresponding action.