Getting started with Rust

May 25, 2015 Written by RajThilak Cofounder & Chief Architect

In this we will get started with the cool language from mozilla - Rust.

We will use functional programming as far as we can. Rust supports it in an elegant way.


rust is a systems programming language that is compiled and hence there is not garbage collection unlike Go. Hence its a super cool/fast language. It has an awesome rubygems like community in

The symantics are little bit different but you’ll get used to it.

Why are we looking at rust ?

We started out to identitfy memory optimized language for our IoT startegy. So we decided to put it action in our commandline rewrite meg

Installing rust

  • Ubuntu

    sudo add-apt-repository ppa:hansjorg/rust

sudo apt-get install rust-stable cargo-nightly
  • Arch linux

    yaourt rust cargo-bin

Your first rust lang library

A rust lang library is something that can be included as a crate in a program we build.

In this case we will pull our cli library called rust-turbo


Let us clone this library locally to ~/megam.

git clone

cargo clean

cargo build

[ram@ramwork:rust-turbo|master]$ cargo build
   	Compiling gcc v0.3.5
   	Compiling hamcrest v0.1.0 		(
Compiling libc v0.1.8
Compiling glob v0.2.10
Compiling strsim v0.3.0
Compiling rustc-serialize v0.3.14
Compiling regex v0.1.30
Compiling term v0.2.7
Compiling log v0.3.1
Compiling time v0.1.25
Compiling env_logger v0.3.1
Compiling docopt v0.6.64
Compiling rust-turbo v0.2.0   (file:///home/ram/code/megam/rust/rust-turbo)

Cool we just built our first library.

Every project that uses cargo as the build tool will include a file called Cargo.toml

  • library ? Huh !

The question is how do you decide if you are building a binary (executable) or a library to be includeded as a crate.

Make a change in the cargo.toml file include the following.

name = "turbo"
path = "src/turbo/"


cargo tests

There are bunch of tests under src/tests that are under the src directory they get run automatically.

Use a matcher like hamcrest to compare the output and expected out to decide on the failure or success of your tests.


In our case cargo will look forward towards a file called turbo/ The details of are

#![cfg_attr(test, deny(warnings))]

#[macro_use] extern crate log;

#[cfg(test)] extern crate hamcrest;
extern crate docopt;
extern crate glob;
extern crate rustc_serialize;
extern crate term;
extern crate time;
extern crate libc;

pub mod util;
pub mod core;
pub mod turbo;

Let us examine the above.

  • extern crate indicates our intention to use an external library eg: logging, docopt (options command line parsing) etc.

  • pub mod util indicates that we intend to include a module named or util/

If you see our source code we have 3 folders util, core, and

You first rust executable

In this case we will pull our cli exec we are building called meg


Let us clone this library locally to ~/megam.

git clone

cargo clean

cargo build

The built binary will be under targets. so run it to your hearts content. eg:

meg version

We have covered setting up rust, and get going with a rust library and an executable. Stay tuned on other design specifics using rusty lang.. Adios.