Showing posts with label rust lang. Show all posts
Showing posts with label rust lang. Show all posts

Thursday, February 20, 2020

gdb tips - PART1

gdb

Some gdb ( GNU debugger) tips

It is very important to understand the debugging tools and their different functionalities to hack the system level programs easily.

How to compile a program to debug in gdb.

  1. compile your C program with -g option

    $cc -g helloworld.c

  2. Open the executable in gdb
    $gdb a.out

  3. Run the program using below commands in gdb prompt using simply ‘r’.
    (gdb)run or (gdb)r

  4. Continue run from breakpoint using ‘c’
    (gdb)c

Setting Breakpoint in gdb.

1.Setting up breakpoint in a line number
$b 10
2. breakpoint setting up in an address
$b *0x7c00

(gdb) b *0x7c00
Breakpoint 1 at 0x7c00
(gdb) c
Continuing.
Thread 1 hit Breakpoint 1, 0x00007c00 in ?? ()

3.Setting up breakpoint in a function
$b main

Switch to tui mode using ctrl+x 2

you can switch to tui ( text gui mode) mode using ctrl+x 2
to view the source file in the same screen.
tui mode

you can ctrl+x 1 to see registers values

ctrl+x n to come back to gdb prompt.

stepping through line and instruction

$si \ steps through instruction
$s \ source level step through next line

Next step over through line

$n
$next

gdb_steps_through_source

Printing variables

$p foo//print the value of foo
$p car // print the value of car variable

$p/x addr //printing hexa decimal values

information about frames and registers

Current function stack frame details can be displayed using the command ‘f’ or ‘frame’
$f
$frame

enter image description here

The registers details or values can be seen using the command ’ info r’

info r
enter image description here

backtrace of call stacks

How can we backtrace all call stacks in gdb?!. This would be very useful command especially when you are debugging big projects source codes.

$bt
shows the backtrace of all call stacks so far.

enter image description here

You can switch to different frames using below command

$f 1
to switch to the first frame
f 5 switch to 5th frame etc

Some of the helpful commands when you are in a function

$info locals
shows the local variable in a function

$info args
shows arguments of the function

getting help on gdb

(gdb) help

List of classes of commands:

aliases – Aliases of other commands
breakpoints – Making program stop at certain points
data – Examining data
files – Specifying and examining files
internals – Maintenance commands
obscure – Obscure features
running – Running the program

(gdb)h breakpoints //provide help on breakpoints

PART 2 -
https://naveendavisv.blogspot.com/2020/04/gdb-debugging-part-2.html

Wednesday, February 12, 2020

Going to back OS/system related computer courses

I tried to read linux kernel and dropped it long back around the year of 2007 or 2008. Whenever I start touching system level code, programming languages becomes alien language for me. I thought of watching again the OS/computer architecture video courses. Luckily come across xv6 (https://en.wikipedia.org/wiki/Xv6)  operating system. This makes/motivating me to give one more attempt to learn the kernel and system level programming.

This one also helps me a lot.
  https://github.com/Babtsov/jos/tree/master/lab1

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, 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

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

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.

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/







Monday, November 26, 2018

Algorithms - sorting algorithms implementation in Rust programming langage

It's important for programmer to do deep dive into algorithms some point of time. but I never tried to do that. As ECE( electronics) branch never needed algorithms in their curriculum, i nicely ignored algorithms . But become unavoidable thing for any kind of application development/programming . its becoming essential to have some ideas or ready made algorithms or approaches in hand to start with some innovative ideas to convert as applications.  So thought of refreshing the algorithms from basics.

If you are learning new language , and trying to solve/implement those basics algorithms , that would be the ideal way to learn them.

And here we are trying to solve/implement some algorithms or problems through Rust programming language.

1. Merge function - https://davnav.github.io/rustgeeks/rust_merge_sortedvectors.html
2.Insert Sort - https://github.com/davnav/rustgeeks/blob/master/rust_insertsort.md
3. Longest string - https://davnav.github.io/rustgeeks/rust_LongestString.html

Tuesday, November 20, 2018

A new documentation started with Algorithms/problem solutions with Rust programming language


Rust for Geeks:


The problems/algorithms can be Easy,Medium or Hard. If you are newbie to programming , you might have noticed most of the schools/colleges start with “C Programming “ language.
But as an alternative , I would recommend, start with Rust programming language. As you all know C was early computer language and created on 1989. Computer programming languages were evolving over the years.So in a way we can say Rust programming languages evolved over 30 years programming language researches. So its better to start with Rust programming language if you are beginning. But you can learn C/other languages also.

so visit my new github page:

Thursday, September 27, 2018

what to do after reading the Rust tutorials !!

if you have read through most of the concepts in Rust and want to build something in Rust, i would suggest below website to start with...

https://mattgathu.github.io/writing-cli-app-rust/

Friday, May 25, 2018

Rust-lang learning continuation...

I tried to start again to read on the Rust-Lang tutorials . I thought , i can start with web development in Rust. Usually I directly jump to advanced side of a language and stop it in a few days :-).  Most of the languages I tried that way are ok to start, But Rust is a little more difficult .

For Rust , directly jumping to web development is really tough without understanding the basics. But I am not going to change that method.( really its like ,going back and forth )


Rocket is similar to Flask or Django .. but Rust syntax is killing me.. I can't blame Rust, as it is trying implement safe code.  Rust compiler is so rude, seems like  it won't allow me to compile a single program :-)

Todays learning:




&'static str

"This basically reads as "a reference to a string slice with the lifetime of static", which means that the string has a lifetime of the global scope"

& is familar symbol for programmers, it is used to reference a variable. Generally to get the address of variable .

just refreshed here - https://www.cs.fsu.edu/~myers/c++/notes/references.html

But what is  ' symbol , this is something new to me. it is the lifetime. other programming languages have the scope of the variable. But those languages compilers are not always smart enough to understand the scope of the variables or in other words Rust-lang push to define the life time of the variables for each functions. ( From this, I hope compiler can do the garbage collection by itself)

read more: https://www.quora.com/What-is-the-meaning-of-in-Rust









Sunday, January 21, 2018

Crates and Modules in Rust Language

I was trying to jump into Rust programming language through Rocket web framework. I know it might be a tough attend to directly start with Rocket without much understanding on the Rust syntax.

I believe I have a minimum understanding on Rust syntax and I thought let learn it reverse way , whenever something comes strange or unknown ,let go reverse and goto the basics.

 So, with that strategy when I jump to Rocket , I came across lot of new things.. Ofcourse Rust has lot of new syntax , terminologies. But since I had some prior knowledge on Flask,Django web frameworks, I did not stunt by the folder structures and initial quick start steps.

but it is really fun learning it in reverse way, lot of new things to understand.

One thing was Crates and Modules. It not a new idea, but it a new term and building it in Rust is quite easy.

We have a good documentation from these from Rust developers.

As per my understanding,

If you know python, we will be using packages there. Similar way we can use Crates to build modules and import them into our program.

I hope below documentation will be enough to read through it and try it.

https://doc.rust-lang.org/book/first-edition/crates-and-modules.html

https://rustbyexample.com/attribute/crate.html

Saturday, July 9, 2016

Rust applicatons

Are searching some of the applications written in Rust? below you have some of the interesting applications.

1. http://www.redox-os.org/  - operating system written in Rust
2. http://zinc.rs/ - Zinc is an experimental attempt to write an ARM stack that would be similar to CMSIS or mbed in capabilities
3. http://maidsafe.net/ - Maidsafe, a company that tries to create an encrypted, completely decentralized "successor" to the internet
4.https://github.com/servo/servo - Servo, the new browser engine being developed by Mozilla 
5. Iron is a high level web framework built in and for Rust, built on hyper.
ironframework.io/doc/iron/

6. wtftw, a tiling window manager - https://kintaro.github.io/rust/window-manager-in-rust-01/

Friday, June 24, 2016

Rust pointer implementaiton

https://aml3.github.io/RustTutorial/html/02.html#Starting_to_Corrode:%3Cbr%3EPointers,_Memory,_Strings,_and_I/O

I am really scared of pointers in C programming itself.. Rust have more features than C for pointers .. :-)


But this really good read the above link on Rust memory management. I believe that there are lot of values for learning these new features .

Monday, May 30, 2016

How to start with RUST LANG in linux machine

I am started hearing about RUST programming language http://doc.rust-lang.org/book/README.html, so thought of give a try 'helloworld' program. The impressive thing is,the language focus on safety,speed and concurrency.

Rust is a systems programming language focused on three goals: safety, speed, and concurrency. It maintains these goals without having a garbage collector, making it a useful language for a number of use cases other languages aren’t good at: embedding in other languages, programs with specific space and time requirements, and writing low-level code, like device drivers and operating systems

There is very good documentation - http://doc.rust-lang.org

Below is the installation steps, helloworld program . I gave a 'Strace' on the program to see any difference.I couldn't understand any difference :-).. i dont know how to find the difference in speed or concurrency.. need to read more ...