Rust - The Language of the future: Rich Type System, Correct and Fast

Learning is a chaotic process sometimes. It's like trying to fix a puzzle with 1k pieces, sometimes you don't know where you are going but you need to feel the vide and let your gut feeling drive you. Being a software engineer means that I need to learn every single day, otherwise I'm devaluing myself and also devaluing the solutions I built and therefore the customer is getting less value at the end of the day. IMHO a software engineer is like a startup that went IPO if it's sexy, healthy, awesome and doing bleeding edge stuff the stocks go up otherwise they might go down. However, for many engineers, the only way to get better in change jobs which I think is ultimately true in the IT industry, however, I would like to ask a question, What's the point to change job if you will be the same? So in order to change you need to change first. Rust language was the last language I learned so far(2019). Every time I learn a language I learn something new and that's improved my thinking and the solutions I built. I Learned Clojure in 2011 for instance, never deliver a Clojure system in production however learning Clojure was super important to improve my functional programming skills in Scala which I do have systems in production since 2011 as well. Clojure was super important to improve my design skills. I have to confess, I'm in love with Rust, not because is the lastest language I learned so far but because it introduces great things that were present in other languages like C, Java, Scala and Go but also Performance with Safety.

I need to be careful when I write the word safety. Because If you are a functional programming engineer you will think in sense of type safety, Rust has that, don't get me wrong but Rust goes beyond that because it has Extreme Performance and Extreme Memory Safety.

Memory Safety

  • Null Pointer Safety: For instance, by default, it won't let you access static global var. Rust by default won't let you change pointer variables and have multiple pointers to the same variable. 
  • Dangling Pointers: With proper context, borrowing, slices and much more.
  • Data Races: Rust provides Safe multi-thread programming model with atomics for instance.
Rust Language also has the keyword called *Unsafe* which means you are doing something that might break or introduce bugs. This is great because by default it disallows bad practices and makes easier to catch these things during a code review session. Rust compiler is your greatest friend it can catch several things for you. Rust compiler is getting faster and faster every day. Coding in Rust by default means favor immutability over mutability which is a very functional programing principal.

Garage Collection Issues

Languages like Java, Scala, Go, C#, Python have Garage Collection. These languages require more CPU and memory power(Specialty Java and Scala :D).  Garbage collectors also introduce common well know issues like No Predictability(Hard for real-time systems),  Limited Concurrency(Global Interceptor Lock - like in Ruby), Larger Code Size(VM or GC must be included), GC Pressure could be very hard(look Cassandra for instance). Rust provides abstractions for system programming with ZERO COST of abstractions. You also can call C(Extern "C") which a syntax that is simpler than C++ for instance.

Past, Present and The Future

Rust was created by Mozilla in 2010. Rust is the secret source Mozilla was with new versions of Firefox which are super faster. Now we have OS written in Rust. Even Microsoft is considering use Rust because 70% of bugs in windows are memory-related. System programing option #1 nowadays is Rust. I love Go language as well. However, Rust is becoming my number #1 option currently. Rust is also the default implementation and engine for WebAssemly and WASMER. Which is the future! WebAssembly is the Future new Docker, so we can run sandbox code anywhere safety and compile the web so we can get the performance and also write web code in any language as long as we generate web assembly. Because WASM is a binary format. Right now besides Mozilla, we also have Yelp and Dropbox using Rust intensively.

Write is Cheap, let's see some Code!

Pattern Matcher is very similar to Scala Pattern Matcher, we can see lots of flexibility.


Rust is also great for Functional Programing: It has closures, Immutability, Type Systems via Strcuts, Traits, and Generics, High Order Function and much more.



Finally, let's see Unsafe in Action, So if you don't use Unsafe block the compiler will fail and you won't get a binary to execute the code.



You can check out the code in my Rust repository on GitHub.

Cheers,
Diego Pacheco


Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java