Getting started with Rust
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
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 crates.io
.
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
-
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
rust-turbo.
Let us clone this library locally to ~/megam.
git clone https://github.com/megamsys/rust-turbo
cargo clean
cargo build
[ram@ramwork:rust-turbo|master]$ cargo build
Compiling gcc v0.3.5
Compiling hamcrest v0.1.0 (https://github.com/carllerche/hamcrest-rust.git#b61fef3e)
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.
[lib]
name = "turbo"
path = "src/turbo/lib.rs"
testing
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.
namespaces
In our case cargo
will look forward towards a file called turbo/lib.rs
. The details of lib.rs
are
#![deny(unused)]
#![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 util.rs or util/mod.rs.
If you see our source code we have 3 folders util
, core
, and turbo.rs
You first rust executable
In this case we will pull our cli exec we are building called meg
meg.
Let us clone this library locally to ~/megam.
git clone https://github.com/megamsys/meg
cargo clean
cargo build
The built binary will be under targets. so run it to your hearts content. eg:
meg versionWe 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.