Thursday, September 19, 2019

A bit research on String type in Rust

string

How did the String Type in Rust define?

There are two type of string types in Rust. Once is &str and other one is “String”.

String is heap allocated, grow able and not null termiated type.
&str is a slice(&[u8] ) that always points to a UTF8 valid sequence.
learn more about type usage here String types

But we are trying to focus on “String” type and how do we search Rust source code to see how this type is implemented.

String type - Heap allocation

Heap allocated memory implies that there will be pointer behind the definition of this type.

Let’s see,

We can get the rust source from below link.

https://doc.rust-lang.org/std/string/struct.String.html

click on the src button, as highlighted in the link.

enter image description here

it will directly take you to the surprising fact that String is simply a vector ( vec ).
String is a Vec<u8>

enter image description here

We know that Vec is also a heap allocated memory. In order to find how “vec” is defined in the source ,we need to search keyword “Vec” in the source code.

enter image description here

click on “src”
enter image description here

Interestingly, you will see the code.

pub struct Vec<T> {
    buf: RawVec<T>,
    len: usize,
}

I haven’t heard about RawVec earlier, but this is coming from : crate::raw_vec::RawVec;

raw-vec is implemeted for handling contiguous heap allocations.

enter image description here

Rawvec is implemented as below using a Unique pointer.

As guessed , we come to know that String type is actually using a pointer.

enter image description here

But what is unique pointer?
It comes from use core::ptr::Unique;

Unique is wapper around a raw non nullable ‘*mut T’.
interesting …

enter image description here

NonZero is wrapper type for raw pointers and integers that never becomes zero or null .

Tuesday, August 27, 2019

FFI tricks in Rust programming Language

Welcome file

How to do a trick on your Rust compiler to allow external (Foreign Language - FFI) functions

Rust is cool programming language which you can easily plugin to other languages ( Foreign Languages) .

Some of the FFI (Foreign language Interface) methods to call rust programs from C programming and Python programming languages are explained here.

Rust - Two things you should know for allowing Foreign language to call

#[no_mangle]
You might see lot of libraries using #[no_mangle] procedural macro.
The #[no_mangle] inform the Rust compiler that this is foreign language function and not to do anything weird with the symbols of this function when compiled because this will called from from other languages. This is needed if you plan on doing any FFI. Not doing so means you won’t be able to reference it in other languages.

extern

The extern keyword is used in two places in Rust. One is in conjunction with the crate keyword to make your Rust code aware of other Rust crates in your project, i.e. extern crate lazy static. The other use is in foreign function interfaces (FFI).

read more extern

how to create a shared object ie .so file from your Rust program ?

We can create a library in Rust using command below command

We are required to update the Cargo.toml

[dependencies]
[lib]
name = "callc"
crate-type = ["staticlib", "cdylib"]

cdylib is important crate type to specifiy in the Cargo.toml.
--crate-type=cdylib, #[crate_type = "cdylib"] - A dynamic system library will be produced. This is used when compiling a dynamic library to be loaded from another language. This output type will create *.so files on Linux, *.dylib files on macOS, and *.dll files on Windows.

Read more on this here linkage

Cargo new callc


//lib.rs
#[no_mangle]
pub extern fn hello(){
  println!("hello world inside rust ");
}
          

as we discussed earlier , since we use #[no_mangle], we can directly go ahead and compile it.
cargo build --release

How to check the generate .so file ?

A file with extension “.so” will generated in you target->release library.
enter image description here

run the below command to verify the function in the shared object.
$nm -D ‘filename.so’ | grep ‘function_name’
enter image description here

Our external function name is available in the shared object file created.

Create a C program for calling the shared object ( .so file)

create C program to call the hello function in your ‘src’ folder.

hello.c

#include<stdio.h>
#include "hello.h" /*header file to be included

/* the main C program to call the hello function
/* hello function is resides in the .so ( shared object) file

int main(void){
   hello();
}

Below header file needs to be created in your ‘src’ folder

hello.h

/*the header include file should have the function declaration.

void hello();

We should link the .so file while compiling the c program. The below command will link and compile the program.

While compiling the C program, link the .so file the ‘release’ folder.

gcc -o hello hello.c -L. -Isrc /home/naveen/rustprojects/callc/target/release/libcallc.so

Your C program should get compiled and 'hello’e executable should be generated.

How do you call a Rust program from Python.

We are going to use the crate pyo3 for creating python modules.

create the projectfolder using cargo
cargo projectname --lib

enter image description here

Your Cargo.toml file should be updated as below in library and dependency sections.

[lib]
name = "string_sum"
crate_type = ["cdylib"]

[dependencies.pyo3]
 version = "0.7.0"
 features = ["extension-module"]

update the lib.rs with the below code:

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
///format the 2 numbers as string
fn sum_as_string(x:usize,y:usize) -> PyResult<String>{
    Ok((x+y).to_string())
}

#[pymodule]
fn string_sum(py:Python,m:&PyModule) -> PyResult<()>{
    m.add_wrapped(wrap_pyfunction!(sum_as_string))?;
    Ok(())
}

You can build the Rust program using Cargo build --release

This will create .so object in your cargo release folder. You might need to rename the file libstring_sum.so to string_sum.so

enter image description here

Now you can start import the module to the python interpreter and starting using the function string_sum

You might need to use python3 or 3+ interpreters for pyo3 to work.
As screenshot shows below , you can import the functions defined in the Rust module and use them in python.

enter image description here

Tuesday, August 20, 2019

One of powerful usage of Enum in Rust - Part1

Welcome file

Rust programming - Enum inside Struct

You might have come across this scenario in programming where you want to restrict a field type to restricted custom types.

Especially when you are working with APIs, the some of the field types can be one of the multiple custom types. You don’t want to accept the types other than defined custom types. enum can help you in these scenarios.
,eg event types can be ‘pullevent’, 'pushevent", ‘keypress’,‘mouseevvent’ etc

Example progarm

#[derive(Debug)]
///enum defined for vechicle type field
enum Oneoftwo{
    Car(String),
    Bus(String),
   
}
///struct defined for vehicle.
///Here we are using the defined enum as a type for a field in the struct
struct Vehicle{
    type1:Oneoftwo,
    price:i32,
}
///implementation for the vechile to create new instances
impl Vehicle {
    fn new(type1:Oneoftwo,price:i32) -> Vehicle{
        Vehicle{
            type1,
            price,
        }
    }
}

///main program to demostrate how to use enum inside a struct
fn main() {
    ///creating instances of the struct object
    let honda = Vehicle::new(Oneoftwo::Car("honda".to_string()),4000);
    let tata = Vehicle::new(Oneoftwo::Bus("Tata".to_string()),10000);
     
    println!("{:?},{:?}",honda.type1,honda.price);
    println!("{:?},{:?}",tata.type1,tata.price);
}


output:
Car("honda"),4000
Bus("Tata"),10000

Sunday, August 11, 2019

Learn Python ,RUST and C - blog post 2

Data Types in Python,Rust and C - PART 1

Understanding Data Types in Python,Rust and C progamming languages.

I am not sure whether I can cover all data types comparison in one blog post. So I might be doing it in multiple blog posts.
Standard Data Types:*
  • Numbers.
  • String.
  • List.
  • Tuple.
  • Dictionary.
Numbers Datatypes
**Python programming **
Type format description
int a = 10 signed integer
long 356L Long integers
float a = 12.34 floating point values
complex a = 3.14J (J) Contains integer in the range 0 to 255
enter image description here
The variable types are dynamically typed in python.So when you assign a value to a variable, python compiler determines the type of the variable.
But once the type is defined , the defined operations can only be performed for that particular type.
but you can change the type of the variable by assigning a different value. So in python it is the developer responsibility to maintain and make sure type of the variable intact through out the program.
For example,
>a = 10;
>a = "naveen" //allowed
>a = a + 1 // not allowed
>a = 11  //allowed
>a = a +1 // now this is allowed
**Rust programming **:
Length signed unsigned
8bit i8 u8
16bit i16 u16
32bit i32 u32
64bit i64 u64
128 bit i128 u128
arch isize usize
signed and unsigned means whether a number can be -ve or +ve.
Each signed variant can store numbers from -(2n -1) to 2n-1
unsigned variants can vary from 0 to 2n-1
eg: u8 means 0 to 28-1 = 128
signed means -28-1 to 28-1
Rust’s floating-point types are f32 and f64, which are 32 bits and 64 bits in size, respectively.
In the below example we assigned a value 129 to varialble ‘b’ which is of ‘i8’ ,so the program won’t get compiled.
fn main(){
   let a:i8 = 127;
   let b:i8= 129;  //this will cause overflow.
   let c:f32=123.32;
   let d:i8 = -127;
   let e:u8 = 127;

  println!("{}.{},{},{},{}",a,d,c,e,b);
}

when you compile the program,

error: literal out of range for `i8`
 --> datatype.rs:3:13
  |
3 |   let b:i8= 129;
  |             ^^^
  |
  = note: #[deny(overflowing_literals)] on by default


String:
Strings in python can be denoted by single quotes(’), double quotes("), triple quotes(""").
name1    = 'naveen'
name2    = "davis"
fullname = """naveen davis
                       vallooran"""
python3
It possible to access the characters in a string using their index in python.
python-string2
nam1[0] prints the character 'n'

Saturday, August 10, 2019

Learn Python ,Rust and C - blog post 1

Learn Python ,Rust and C - session 1

Learning Python ,Rust , C programming together

I have seen in different forums that people ask following questions " Why should I learn multiple programming language ?" or “Should I learn C or Python or JavaScript ?”.
My opinion would be , you can concentrate on one language and learn it deep. But as a developer some point of time it is important that to learn multiple programming languages and understand the difference and know where these languages are strong. This would helps you take decision on which language to use for your application.

Python Hello world : you can run this program from a python interpreter or save the program in in file and run with python.
install python - python

//python programming 
print("hello world")

enter image description here

Rust Hello world
The below Rust program needs to be saved in filename.rs and compiled with Rust compiler.t
Please follow the link for Rust installation How to install rust

///Rust programming 
fn main(){
 println!("hello world");
}

enter image description here

or You can create a rust programs using Cargo.
The link will help you to install cargo - Cargo
enter image description here

C Hello world
You preferred to have GCC compiler to generate C program executable.
installation -
Ubuntu GCC installation
Windows GCC

The program can be saved in a helloworld.c and compile and execute as below .

enter image description here

#include<stdio.h>
int main(){
    printf("hello world\n");
}

Python Rust C
Interpreting language compiled language compiled language
platform independent cross platform
individual building or compilation for each platform that it supports,
platform dependent
Strongly typed
dynamically typed
strongly typed and dynamically typed weekly typed and statically typed
objected oriented Object-Oriented Programming is a way of modeling programs that originated with Simula in the 1960s and became popular with C++ in the 1990s. There are many competing definitions for what counts as OOP, and under some definitions, Rust is object-oriented; under other definitions, it is not. structural language
inbuilt garbage collector no separate garbage collector,but memory management is done automatically in compile time manual memory management

Tuesday, May 28, 2019

Rust Closure - PART2

Rust Closure - PART2.html

Closure Rust - Cont…

FnMut Trait

This is a continuation of the previous post ( https://naveendavisv.blogspot.com/2019/05/what-is-closures-how-can-it-be-used-in.html) on closures in rust.
Last post we defined the closures as Fn(i32) -> i32 when we want to pass the closure to a function.
Fn trait borrows values from the environment immutably.
But now we will see what is FnMut? .
FnMut can change the environment because it mutably borrows values.
fn double(mut db1:T)
   where T:FnMut() -> i32 {
     println!("{:?}",db1());
  }

fn main() {

  let mut x = 10;
  let  db = || { x*=2; x};
  double(db);
  println!("{:?}",x);
}
The above example, the ‘x’ value will recalculated when we call the closure inside the function ( double)
If we want to change a value in the environment that closure is enclosing, the FnMut trait can be used to define the closure.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5102dff068bc256a59bc9c8c9bf3f92f

Cacher struct with closure.

We will try to build a Cacher Structure as explained in the rust book - https://doc.rust-lang.org/book/ch13-01-closures.html
The need for Cacher structure is explained in the book.
Final part of Cacher structure chapter , the author ask us to create the struct with HaspMap to store the calculated value
I will try to explain the Code wrote in Rust here.
How nice would it be if we can cache the computation intensive function and store it's calculated values in a struct !!
A data structure with a 'Closure' and 'Hashmap' will serve the purpose very nicely.

How do we define Cacher Structure with HashMap ?

We can call data structure as Cacher as it cache/store the result values.
Normally we don’t define the parameter type or return type of a Closure in Rust. But it is must to explicitly declare the field types of a Struct , so we need to explicitly define the 'T' where T is type of the Closure .Rust Compiler needs to understand all the field types of structures in order to allocate memory
resultMap field is Box (A pointer type for heap allocation - https://doc.rust-lang.org/std/boxed/struct.Box.html) reference to Hashmap.
If you don’t know how much memory you are going to use, one option would be “Box” type. Here we defined the field resultMao as Box reference to a HaspMap .
struct Cacher
  where T:Fn(u32) -> u32
  {
    square:T,
    resultMap:Box>,
  }

How do we implement the Cacher Struct ?

We defined the Cacher Struct , the next step would be we need to implement the Struct with methods.
one method that we would require is “new” . This method creates Objects of the Struct. Another one would be “value” - to get the value from the resultMap. As resultMap is reference to a HashMap , we can use “insert” method to add key and value pairs to the hashmap.
Also we can use “get” method to get a value for a key from the haspmap.
impl Cacher
  where T:Fn(u32) -> u32{
  fn new(square:T,mut resultMap: HashMap) -> Cacher{
    Cacher{
              square,
              resultMap:Box::new(resultMap),
           }
  }

  fn value(&mut self,x:u32) -> u32 {


     match self.resultMap.get(&x){
       Some(v) => *v,

       None    => {
                   let v = (self.square)(x);
                      self.resultMap.insert(x,v);
                   v
                  },
    }
  }
}

Rust Playground - https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d131716d3620b60521a2af2067a32dcd

Tuesday, May 21, 2019

What is a Closure ? How can it be used in Rust programming ? - PART1

Closures in Rust

Closure is record storing a function along with it environment variables. You can read about closures in wiki

Difference between Closure and a function.

function definition:
Note: we have to explicitly pass the type and variable.
fn add (x:u32) -> u32 { x +1 }

Closure definition in Rust:
Note: We can follow any one of the following syntax to define a Closure
//explicitly defining types
let add = |x:u32| -> u32 { x +1 }
or
//the compiler dynamically type the Closure variables
let add = |x| -> {x+1}
or
//ignoring curly braces if you have only one statement
let add = |x| -> x+1

We will go through some example for Closure and discuss its features

Example#1 - passing Closure to a function

Below example shows the closure is defined with parameter ’ x ’ passed from environment where it is called. Closure is enclosed in a variable called square_x here.
We can see that value of the variable x is 2 . In the main program, first we define the Closure and then it is called from square_x(x) in println! statement.
square_x is the Closure.
When Closure looks for it's required variable's 'x' value for calculation, Closure its self will be enclosed with its required variables and understand that 'x' is parameter and it need to get its value from lexical environment which is 2 . And it perform the calculation: x*x - square of 2 which is 4.

The second print statement execute inside the function square . When we call the square(square_x) function, Closure invoked and looks for its lexical environment and find the value for parameter ‘x’ variable.The ‘x’ variable value is 3 over there, so the Closure calculate the square of 3 which is 9 .

fn square<T>(sq:T)
 where T:Fn(i32) -> i32
  {
    let  x = 3;
    println!("square called from insidefn value = {}",sq(x));
  }

fn main() {

    let mut x = 2;
    let square_x  =  |x| x*x;
    println!("square of x = {}",square_x(x));
   

    square(square_x);

}

From what we learned , Closure doesn’t need to specify it’s Parameters type or Return type. But in the (square) function definition, we explicitly defined ‘T’ as Fn(i32) -> i32.
Because for a function, it is necessary to define it's parameters type.

run in Rust Playground -
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c7dafb2aa3b7f7f83fc7490be6f30925

Example 2 # What will happen if we are not passing variable

This example we remove the passing parameter, to make the Closure as no-parameters.

let sq = || x*x;

But Closure has the property to get all its variables from its lexical environment . When you pass it to a function, it actually pass the Closure and along with variables enclosed.

fn square<T>(sq:T)
 where T:Fn() -> i32
  {
    let  x = 3;
    println!("square called from insidefn value = {}",sq());
  }

fn main() {
    let x = 2;    
    let square_x  =  || x*x;
    println!("square of x = {}",square_x());
    square(square_x);
 
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=adc2ce2cb7fc2f73ddee5e1cbfe10a9d

Example 3- What will happen if you pass different variable to Closure in the above example

fn square<T>(sq:T)
 where T:Fn(i32) -> i32
  {
    let  x = 3;
    println!("square called from insidefn value = {}",sq(x));
  }

fn main() {

    let x = 2;
    let square_x  =  |y| x*x;

    square(square_x);
    println!("square of x = {}",square_x(x));
}

Can you guess the output ?Hint : Closure is record which stores function along with its environment variables.
run the example above and check out the output.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7763c76893f87426f975db3eafab5e0f

Reference https://doc.rust-lang.org/rust-by-example/fn/closures.html

https://doc.rust-lang.org/book/ch13-00-functional-features.html

Friday, April 12, 2019

Concurrency in Rust programming language- PART1

rust concurrency

Concurrency in Rust - PART 1

As per Rust documentation, concurrency in Rust is Fearless concurrency. Handling concurrency in a safe and efficient way is one of the Rust goal. The Ownership is the back bone of rust concurrency.

Concurrency means multiple computations at the same time.

  • In fact, concurrency is essential part of modern programming

eg > websites should handle multiple users at the same time
Graphical user interface need to do some background work when users are not interrupting the screens.
Multiple applications needs to run on the cloud (server)
Travel reservation system
Many algorithms can be broken down to concurrent parts -
merge sort
quick sort
summing a list by summing fragments in parallel

Concurrency can be achieved in Hardware level and software level. Here we will be talking about software concurrency and what makes Rust an efficient programming language to handle this.

Hardware examples:

  • A single processor can have multiple execution pipelines (but only one set of registers)
  • A processor can have multiple cores (multicore)
  • A computer can have multiple processors
  • A network can have multiple computers (Grid computing happens on these)
  • An internet has multiple networks
  • All combinations of the above

Software examples:

  • Multiprogramming, or running a lot of programs concurrently (the O.S. has to multiplex their execution on available processors). For example:
    • downloading a file
    • listening to streaming audio
    • having a clock running
    • chatting
    • analyzing data from some weird experiment
    • streaming a movie

Two Models for Concurrent Programming

  • Shared Memory - In this case, the same memory will be shared amount the processors /threads or programs.
    A and B programs can be trying to use the same object .
    A and B threads can be using the same file system that they can read and write. etc…
  • Message passing -
    There will be a transmitter and a receiver in this model. They communicate each other through a communication channel. Modules sends the messages and the incoming messages will be queued up for modules.
    A web browser and web server communication is an example. Web browser client request for a connection and web page, the server sends the data back to browser.

Jumping directly into Rust concurrency

Process. A process is an instance of a running program that is isolated from other processes on the same machine. In particular, it has its own private section of the machine’s memory.
Thread. A thread is a locus of control inside a running program. Think of it as a place in the program that is being run, plus the stack of method calls that led to that place to which it will be necessary to return through.
Threads are automatically ready for shared memory, because threads share all the memory in the process. It needs special effort to get “thread-local” memory that’s private to a single thread. It’s also necessary to set up message-passing explicitly, by creating and using queue data structures.

use::std::thread;

fn main() {

// spawning a new thread
    let handle = thread::spawn(||{
        loop {
                println!("thread1");
        }
    });

//looping the main thread
    loop{
        println!("thread2");
    }


}

thread::spawn function creates the new thread and returns a handle to it. The parameter would be a closure . Here it is an infinite loop that prints thread1.
You will see thread1 and thread2 printing in the screen. Unfortunately I could not get thread2 in the screenshot!

Thread 1 and Thread 2

For listing the threads running under the process, you can use below command in linux
get the process id using command
ps -e
then use top -H -p processid
The memory and CPU usage for each threads can be listed using above command.

threads in linux Rust programming

Run the below code and see the output …
The program just print “hello world”. It will not wait for the inner thread to complete and print the vector.

use::std::thread;
fn main() {
    let v = vec![1,2,3];
    let handle = thread::spawn(||{
                println!("{:?}",v);
    });
//    handle.join().unwrap();
    println!("Hello, world!");
}

Now remove comments (//) from handle.join().unwrap(); and run the program. You would assume the main thread wait for the inner thread to print the vector values as handle.join() makes main thread to wait/join with inner thread.

But,
We do get a compile time error . The error says the variable vector “v” does not live long enough. If the main thread complete without waiting for inner thread to complete (as we are seen above). The borrowed vector points to wrong memory when its tries to execute. Here Rust help us.

Rust compiler caught that scenario and threw an error .

   Compiling concurrent-proj2 v0.1.0 (/home/naveen/rustprojects/concurrent-proj2)
error[E0597]: `v` does not live long enough                                                                                                    
  --> src/main.rs:11:19                                                                                                                        
   |                                                                                                                                           
10 |     let handle = thread::spawn(||{                                                                                                        
   |                                -- value captured here                                                                                     
11 |         println!("{:?}",v);                                                                                                               
   |                         ^ borrowed value does not live long enough                                                                        
...                                                                                                                                            
16 | }                                                                                                                                         
   | - `v` dropped here while still borrowed

We can use the move keyword to transfer the ownership of the variables that we are going to use in the inner thread from the environment.

Below program prints both vector values and helloworld.

use::std::thread;
fn main() {
    let v = vec![1,2,3];
    let handle = thread::spawn(move ||{
                println!("{:?}",v);
    });

   handle.join().unwrap();
    println!("Hello, world!");
}
    

Threads Flow diagram

vector 'v'
handle
main thread
inner thread
main thread
handle.join - wait for the inner thread to finish
main thread get completed

Wednesday, March 13, 2019

Have you heard about Competitive programming ?

---

Competitive Programming

If you are looking for job in Google,Amazon or facebook as programmer or developer/designer, definitely you will be familiar with the word "Competitive programming ".

I thought of sharing some of my thoughts on "competitive programming ".
As per wikipedia - Competitive programming is a mind sport usually held over the Internet or a local network. https://en.wikipedia.org/wiki/Competitive_programming

As per definition it’s a “sports”

I heard about hackerrank 3 or 4 years back. I got surprised seeing the success of hackerrank. Definitely its very useful website those who are trying to understand different algorithms/concepts/problems in computer science. After that there was a rain of similar websites … Codechef, CodeByte etc …etc … I don’t know which one of these came first.

But I would suggest nothing harm in login those websites and try to solve some of the problems . Don’t get disappointed /discouraged if you are not able to solve the questions. you don’t need to be a competitive programmer to be a programmer.

I am not huge fan of competitive programming. But it improves your programming skill in multiple perspective and definitely you might need to have this skill for getting into Product based Software companies like Google, Amazon and facebook etc . When I started trying to solve some of the problems in code force , I thought I would require PHD in mathematics to understand the problems . Most of them will be really tough mathematical problems. But if you start from easy , medium and then to difficult once, I hope you won’t need to run away from it :-)

Let me start with advantages of Competitive programming

  • You are going to get good understanding on the Algorithms and data structures .

  • You will get to know different areas in computer science - number theory,set theory,graph theory,string analysis etc. If you ask me , Are these mathematics subjects? I would need to say “YES”. But the fact is you need to have good understanding on these areas in order to develop something unique. ( This is debatable subject) , you can develop innovative or useful applications without much deep understanding on these areas.

  • definitely it’s a plus point for you to get into Software companies.

  • This gives you the ability to approach problems in different ways and helps to get the optimized one.

  • Definitely you will come to time complexity O(n) and space complexity in computer programming :-)

  • Some of the competitive programming sites gives you only limited time to solve the problem. When you start solving the problems in time bound question ,this helps a lot in interviews. You will be able to face the software companies interviews more efficiently.

I think I wrote some advantages , so let me mention some of the websites you can try this .

In my opinion you don’t need to be competitive programmer to become a programmer/developer.

Competitive programming Drawbacks:

  • First of all , its not a technology or programming style or skill that every programmer is needed. Everybody know we need good algorithms to perform the program well. Reading,writing,debugging programs will makes you skillful programmer. Sometimes competitiveness makes to take decision immediately or jump into conclusion quickly.

  • For getting into competitive programming there are some prerequisites - good mathematical knowledge, Computer languages that the websites are using, good knowledge in computer science concepts.

  • If you are trying to learn a programming language from basics (eg: C programming ), I don’t think those sites are the starting point. Those who are good in C, C++,python etc languages , these websites will be a good place to play around.

  • Competitive programming will not take you to most of the computer applications areas .
    eg: Those who are interested in embedded systems , will not get any knowledge on peripheral or architecture of the micro controller/processor from this. or those who are all interested website designing and development not going to get much from competitive programming apart from understanding on the algorithms or logical thinking.

  • Nowadays Application building is like “building blocks” game. More you know about libraries and modules on the area, you will be succeeded in building some good applications.

  • Software life cycle is totally different - As you know it start with requirement gathering,designing,coding,unit testing, functional testing and User acceptance testing and deployment etc. I don’t see competitive programming give much importance to designing and collaboration and testing.

  • If you look at object oriented programming or functional programming , they are a programming style that every programmer should be aware of .

  • You can build innovative applications without competitive programming skills.

Conclusion

Please don’t consider Competitive programming as waste of time. It will definitely improve knowledge on algorithms and computer theories. Keep improving your programming skill on competitive perspective as well. Also be ware of that there are lot of other areas in programming where we can focus on application building or we can playground. As I said earlier You don’t need to be competitive programmer to be developer/programmer.

Please share your thoughts and comments. Thanks!

Tuesday, March 12, 2019

Rust Iterators

iterators

Iterators in Rust programming

Nowadays Iterators are every programmer’s tool in hand.When I program array of objects/strings in C programming , I always wish that I could have some tool/function/operator where we can iterate through these objects or strings(words). “for” loop or “while” loop are going through array index in C programming .

If you are storing words in Array it become 2 dimensional array. So iterating through “words” become even more difficult in C programming .But it will be convenient if you can iterate through the types values/objects in the array. eg: Array(“I”,“like”,“rust”,“programming”) …if you can just iterate through the words rather than reading through the letters, it will be easy to make the words manipulations.

It is Functional programming style where we can just tell the program to iterate through the specified type .
Another use case would be eg: In the given Array (2,4,6,8) of elements if we want to find cube for all the elements in the array and then filter if cube is even number.

For conventional Loop structure we will be iterating through each element and find cube then check and filter if it is even. How great it would be if we can have something like below give us the same output number/maths calculations

example: array.iter().filter(x/2==0).collect()

eg2: words or sentence manipulation

array.iter().filter( words_contains_vowels)

It improves the readability of the program a lot. This functional programming style improves the readability ,but at the same time we can’t compromise the performance . Since they are as fast as our native loops , they are “Zero cost abstraction” in Rust ## iter from standard library The standard library std::iter provides Trait is Iterator. You want to build iterator any Struct ,

you can implement Iterator on your collection with below trait

trait Iterator { type Item fn next(&mut self) ->Option(Self::Item); }

In the below example, suppose if I have an input stream coming from outside and I want to iterator over word by word

//the structure which store the words and index of the words
//note :Since we are using String slice reference , the life time needs to provided explicitly 

struct Mystream<'a>{
    words: Vec<&'a str>,
    index:usize
}

//implementing the structure with methods "new" and "next_word"
//"new" method create a structure object with words generated from  stream
//next_word method calls next method in the Iterator trait

impl <'a>Mystream<'a>{

//"new" method get the string and split them into words vector
//index will be zero for object, but we will increment it in each iteration and use it as indexing the vector
    fn new(stream:&'a str) -> Mystream{
        Mystream{
             words : stream.split(' ').collect(),
             index:0,
        }
    }

//next_word method calls the "next" method from the iterator trait    
    
    fn next_word(&mut self) ->Option<&'a str>{
            self.next()
    }

}

//standard implementation for Iterator for "Mystream" struct

impl <'a>Iterator for Mystream<'a>{
    type Item = &'a str;
    
    fn next(&mut self) -> Option<&'a str>{
            self.index +=1;
            match Some(self.words[self.index]){
                Some(word) => Some(word),
                None => None,
            }
    }
}



fn main(){
    
    let mut mystream = Mystream::new("I like to implement Rust Iterator for my stream");
    let mut len = mystream.words.len();
    println!("{:?},{}",mystream.words[0],len);
    len = len -1;
    while len > 0 {
    
         println!("{:?},{}",mystream.next_word().unwrap(),len);
         len = len -1;
    }
    
}

Not sure above example is tough one, but let me try to explain it… Let assume we have a stream of words coming from input port, our task is to store it in a struct as words .(ie: split the sentence into words). I tried to explain most of the things in the comment section. But still we will go through it … The implementation of Iterator for “Mystream” struct is important part in the code. For understanding Iterator implementation you should know what is trait and struct creation and implementation of struct in rust

Iterator

If look at the Iterator trait documentation, they ask us to create an associated type Item (Associated types are a way of associating a type placeholder with a trait such that the trait method definitions can use these placeholder types in their signatures.) and required method “next

The next method passing parameter and return parameters are mentioned in the example.

As a general rule , for any Trait Rust documentation you will get the required methods or associated types needs to implemented for using it for your struct. Also we need to implement that Trait for our struct.
This case,

impl <'a>Iterator for Mystream<'a>

Since we are using vector of string slice(a reference type), we need to provide life time for it and struct.

Here we have given the associated type Item as &'a str (string slice) which means we are planning to iterate through string slice when the next method calls.

Return type for next method is Option <Self:Item> which means we need use match keyword to return Some(word) if there is value and None if nothing.

In the case Collection types: we can use the
Array,Vec, Hash etc with iter metod.



use std::collections::HashMap;

fn main(){
    let arr = [1,2,3];
    let v = vec![4,5,6];
    let mut h = HashMap::new();
    h.insert(1,"naveen");
    h.insert(2,"davis");
    h.insert(3,"tom");
    println!("{:?}",arr.iter());
    
    for i in arr.iter(){
        println!("{}",i);
    }
    
    for i in v.iter(){
        println!("{}",i);
    }
    
    for (i,name) in h.iter(){
        println!("{:?}",name);
    }
    
    println!("{:?}",arr.iter().next()); //this is going print only first element
    println!("{:?}",arr.iter().next()); //this is going print only first element
    println!("{:?}",arr.iter().next()); //this is going print only first element
    println!("{:?}",arr.iter().next()); //this is going print only first element
    
    println!("{:?}",h.iter().next());  //this is going print only first element
    println!("{:?}",h.iter().next());  //this is going print only first element
    println!("{:?}",h.iter().next());  //this is going print only first element
    println!("{:?}",h.iter().next());  //this is going print only first element
}
iterator_cont

Iter

We can iterate through collection in 3 ways

  • iter(), which iterates over &T.
  • iter_mut(), which iterates over &mut T.
  • into_iter(), which iterates over T.

iterate through immutable reference , in this case we are not supposed to change the value and but we using only reference to value

Rule one: any borrow must last for a scope no greater than that of the owner
Rule two: you may have EITHER 1+ immutable borrows OR EXACTLY 1 mutable borrow

What happens when we compile this code ?
If you have already gone through some Rust program before, you will shout immediately  "compile time" error. As the vector moved in the first for loop and ownership has transfered, so when you try to use it again in the main the value was already dropped. We can use reference to the values to avoid this problem.

True . Rust won't allow to 
fn main() {
   
   let v = vec![3,4,5,7];
   
   for i in v {
      
            println!("{:?}",i);
   }
   
   for i in v {
       println!("{}",i);
   }
   
}

```rust_errors
use of moved value: `v`
  --> src/main.rs:10:13
   |
5  |    for i in v {
   |             - value moved here
...
10 |    for i in v {
   |             ^ value used here after move

In the below example , we solve above problem passing reference to for loop,
You can either use v.iter() or &v


fn main() {
   
   let v = vec![3,4,5,7];
   
   for i in &v {
      
            println!("{:?}",i);
   }
   
   for i in v.iter() {
       println!("{}",i);
   }
   
   println!("{:?}",v);
   
}

If you really want to pass the ownership to the for loop,
use either v or v.into_iter()


fn main() {
   
   let v = vec![3,4,5,7];

   for i in v.into_iter() {
       println!("{}",i);
   }
   
   println!("{:?}",v);
   
}
```rust_errors
 for i in v.into_iter() {
   |             - value moved here
...
11 |    println!("{:?}",v);
   |                    ^ value borrowed here after move
   |
   = note: move occurs because `v` has type `std::vec::Vec<i32>`

Another scenario is you want to update or change the value inside the for loop
you can either pass &mut v or you can use v.iter_mut()

fn main() {
   
   let mut v = vec![3,4,5,7];
   
   for i in &mut v {
      
        *i +=1;
       println!("{:?}",i);
   }
   
   println!("\nsecond iteration to change values\n");
   for i in v.iter_mut() {
      
        *i +=1;
       println!("{:?}",i);
   }
   
   println!("{:?}",v);
   
}

IntoIterator

If you want to use ’ for’ loop in your type, you will need to build the trait IntoIterator for the type.

We will see how to implement an IntoIterator for our type.

IntoIterator

Let build a ‘for’ loop for your Struct

How nice it would be if you can do a for loop which iterate through

[derive(Debug)]
struct Sentence{
    words: Vec<String>,
}

impl IntoIterator for Sentence {
    type Item = String;
    type IntoIter = std::vec::IntoIter<std::string::String>;
    fn into_iter(self) -> Self::IntoIter {
        self.words.into_iter()
    }
}


fn main() {

    let sentence = Sentence { 
        words: vec!["one".to_string(), "two".to_string()]
    };
    
    for word in sentence{
        println!("{}",word.contains("w"))
    }
   
}
Standard Output
false
true

In the above example we are creating Sentence Struct which our type. In that we are storing words as Vector elements.

As we discussed in IntoIterator trait implementation, we need to implement the trait for our type Sentence

So two associated types are required ,

one is Item which is the item we are iterating through.
second is IntoIter ie in which type we are iterating through. here it is std::vec::IntoIterstd::string::String

Note: if it were a vector of integers , it would be std::vec::IntoIter

The required method for IntoIterator trait is into_iter which is also implemented with Rust document mentioned parameter and return type.

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.


Sunday, February 3, 2019

Building a Tree DataStructure in Rust programming language

Building Tree data structure gives more understanding on "Struct" and Box pointers in Rust.

Tree data structure is recurrences of Nodes of same Type where each Node normally will have  "value" field and Pointer pointer to the next node.

This is a little challenging to build the Tree in Rust as Rust won't allow to allocate recurring type as their memory space can not predetermined. But we can use  smart pointer BOX for it.

And implementing methods for the 'Tree' will be interesting.

Please find the problem and solution here in github,

https://github.com/davnav/rustgeeks/blob/master/tree_rust.md




Reference: https://matthias-endler.de/2017/boxes-and-trees/