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

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.

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

Cargo.toml

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
how_old method used to know the years since a show was released
return keyword can be omitted
use return keyword
  • 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
Alias used to name a vector of shows
Parser trait to be implemented by csv parser
String variables could use this new function to_vector

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.

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.

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

Source code

The code of the program can be cloned from here

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.

  • Code an implementation for loading the file netflix.json in resources directory. The command should be

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store