First thoughts about Rust

Introduction

I wanted to try new programming language. A language that is trivial and complex at the same time. Trivial to write fast, complex when struggling with performance or when you want state of art architecture.

Baby steps

This week I decided to try Rust . Rust is an expression-based language. What this mean? In general expression is collection of symbols that jointly express a quantity (or simply expression produce at least one value). But there are also statements which may be smallest standalone elements of an programming language or in other words statements are building blocks of the program.
In Rust everything is an expression, but in general we have two kind of statements. First is an binding statement (for example statement declaration) and second is expression statement, which purpose is to turn any expression into statement (for example adding ; at the end of the line). Why I even write this? Look at this if example which is statement.

If expression
1
let y = if x == 5 { 10 } else { 15 }; // y: i32

Notice that there are no semicolons after 10 and 15. This means that those are expression, and ‘if’ is an expression to, which mean that you can assign if result to y.

The basic program

After reading the first part of the Rust Book I decided to write my first program. I decided to write quick sort. Here is the code.
Lessons learned:

  • Writing a program without IDE and relay only on docs and error codes its very valuable for your learning progress.
  • Cargo, which is Rust build system is intuitive. Out of the box test and benchmark support is another advantage.
  • Primitives like in C. Good when struggling with performance. Bad when want to write fast.
  • Pointers like in C. Dangerous, but powerful toy. In next section there is a few words about how Rust extended pointer that I knew from C.
  • I didn’t like the ‘newtype’, that let you create a new type that’s similar to another one. The concept is good, but after this example, I would say I won’t use it (in most of the cases), because cost of difficulty in extracting value, is not worth providing this kind of type safe:
1
2
3
let length = Inches(3);  
let Inches(integer_length) = length;
println!("In inches size {}", length);
  • I didn’t like that Rust has two main types of strings: &str and String.

Memory management

Rust is about performance. Many of abstractions are done at compile time. There is said that new programers are fighting with compiler. I can confirm that. Once you gain more experience, it is all becoming easy for you. I have to admit that I’ve similar situation with Scala.

Experience of learning my first programming language came to me when in book pass-reference-by-value was discussed.
Rust take pointers to the whole new level. There are mutable references, boxes and more.
But in fact pointers are an introduction to Rust memory aspect such as: Ownership, Borrowing, Boxes and Lifetimes.

I’ve great time playing around and checking what will work and what not. This is a recommended part of the book and this article for further reading.

For me it was good learning journey to read and try new Language. What will be next? Maybe Go or Haskell.

Photo credit: Rust Car, Rust Nut