3 days playing with Rust (and I love it!)

This article is mainly focused for those developers who haven’t tried Rust yet or they are starting to play with it.

It has been written after 3 days playing with Rust .So please… don’t be so hard with me! I’m a beginner & self-though guy that just wants to share with others what is learning!

About this article

In this article we will build a program that retrieves a csv file as input (available shows in Netflix) and it performs some queries to the dataset.

In addition to this, I’ll go through the most interesting things that I’ve learnt these days; you will also find some useful links.

Why Rust?

Because I want to do some researches on IoT. Embedded systems can be programmed with Rust easily.

Setup the environment

It’s very straightforward, just follow the below links:

Cargo

Basically Cargo is the Rust packages manger. Cargo is for Rust like Maven is for Java, Npm for Node. or sbt for Scala

There’s a Cargo.toml in the root of any Rust project. This file could look like this:

Cargo.toml

We could have several executables programs, and they must be defined in the bin section. Have a look at the below link to know about how cargo works.

OOP

Objects are defined with keyword struct. By default, the structs and attributes visibility is private. To make them public we just need to set the keyword pub.

Struct definition used to load each row in our csv file

Methods: Methods implementations are in an impl block.

how_old method used to know the years since a show was released
  • Each statement ends with semicolon. If the last statement in a function doesn’t end with semicolon, it will be interpreted like the return of the function. The both following examples are the same.
return keyword can be omitted
use return keyword
  • self keyword permits access to the object attributes and methods.
  • By default, the variables are inmutables. To make them mutables we need to use the keyword mut.
error: cannot borrow `items` as mutable, as it is not declared as mutable
Correct definition of variable items

Types: We can easily define new types (or alias)

Alias used to name a vector of shows

Traits: Similar to interfaces in some points, but they provides extra functionality.

The traits contains the signature of the methods to be implemented. On the other hand default implementation can be defined in the trait.

Parser trait to be implemented by csv parser

On the other hand, we can also provide traits implementations for existing types.

String variables could use this new function to_vector

Generics: Generics are supported in Rust (Golang… you’re so late). This permits as build more reusable code. The last example in section Traits contains a use case.

Imports

We can import modules by making use of keyword use. Syntax to import modules in Rust is powerful

We can define mods, structs or just functions

Custom errors

Writing our own business error is useful to write code easy to read. To do this, we just need to implement the traits Display and Error for our struct.

FileNotFound error definition
Throwing our own errors

Closures

Closures in Rust, also called lambda expressions or lambdas, are functions that can capture the enclosing environment. For example, a closure that captures the x variable:

Code organization

As in many other languages you’ll find several articles about which one is the best way to organize a rust project.

Interesting link:

Macros

I haven’t done any research on this yet, but It’s something that I want to try as soon as possible. I guess It works like in other languages such as Elixir or Ruby.

Meta programming is so powerful.

Functional programming

Rust provides Higher Order Functions (HOF). These are functions that take one or more functions and/or produce a more useful function. HOFs and lazy iterators give Rust its functional flavor

Sample of use in Rust

Interesting link:

Source code

The code of the program can be cloned from here

https://github.com/wesovilabs/rust_introduction/tree/master

Clone the repository and once the Rust environment is ready jrun the below command

cargo run resources/nextflix.csv

The output should be:

I encourage you to implement JSonLoader or XMLLoader to put in practice what you learnt in this article. Code can be easily extensible.

  • Documentation of crate serde (used to serialize the domain) can be found here https://serde.rs/

What can you do next?

Books & articles are fine to get an idea about technology, but actually you only learn if you put it into practice what you read.

  • Create a new executable file. (Modify Cargo.toml and create a new file in bin directory)

cargo run — bin mydemo resources/nextflix.csv

  • Print the title (in uppercase) of the shows whose release date is lees than 3 years.(and any other query that you want to try).
  • Code an implementation for loading the file netflix.json in resources directory. The command should be

cargo run resources/nextflix.json

Must read articles & tutorials

My feeling

Rust looks so great and It provides excellent mechanisms to build Software. I wish I could learn fast and being able to go through the embedded programming tutorials soon.

Even the documentation is so great, and the language takes the best of several programming languages, I’m afraid its success will depend on how many frameworks to build commercial Software appear in upcoming years.

Sadly the Rust community is not so big, and to be honest, I don’t know companies which want to bet for this kind of challenges…

If tech can change the world... why don't we use it to make it better?