Having fun with Rust



I',m in love with Rust πŸ’“ since 2018. Last year I spend a great amount of time architecture and designing systems with Rust and also Learning and coding. Here are some of my POCs. I've also done several posts about Rust. So this is another one, today I want to show another lick ass things you can do with Rust. No need to say that Rust is Fast and kicks ass but today I will show so cool code I was doing with Rust by coding some Euler with Rust and Java, In comparison, some stuff in Java is actually is better than rust but most of the things are better in Rust. It takes some time to get used to Rust but once you do you will see that the Compiler is just completely amazing and helps a lot. I',m coding rust with VSCode editor thanks to RLS.  One of the features I want to show you guys is called If Let and for me is a killer feature it's super cool.  So let's get started!

Talk is Cheap, Show me the code


I've been playing with Euler with Java and Rust in order to have a fair comparison with some "real" problems that are quite interesting. So I will be definitely posting more about this subject. So this is Euler #2 problem which you need to calculate the Fibonacci sequence for numbers that are EVEN and also <=4M.  In order to do it so I had to implement memoization technique otherwise, my CPU would be completely fried by now :-)

In order to do the Memoization(Also know for common mortals like us: Cache) I used a HashMap. So why Standard HashMap? There is a Sexy new HashMap implementation in Town called HashBrown(OH BOY I miss the American Brunch during this COVID-19) Which is the Google Ported implementation of C++ Swiss Table. So since Rust 1.3.6(I'm using 1.4.3 nighly by now) is part of the STD lib in Rust!



So the cool things about this code are, besides the fact I'm using the Swiss Table Cache implementation and the code its that I'm using 2 very interesting features which are the Pattern Matcher and the IF LET.  For the Pattern Matcher I',m doing recursion and it's very clean and cool as you guys can see it.

The IF LET is another killer feature so At the beginning on the Fibonacci I need to check if the number is on the cache so there is why we use the IF LET which is do a very fast and concise Small Pattern matcher and basically says check if the cache has this key called n. If the key is there give me Some valued with the variable f. Them I have code inside {} which just returns the value. This is quite handy because I dont need to write a full Pattern Matcher there and it's short and concise.

You might realize the Cache comes by parameter to the function so I play nice with Rust borrow and ownership rules and dont need to use Unsafe{}. In Java, you would probably have the cache as a private static final and do not pass the cache as a parameter to the Fibonacci function. So In order to avoid usage of Unsafe{} I made that way.

One of the many things I like with Rust is that it makes the code Obvious, No Implicits(Like I use to not appreciate in Scala), No Crazy Aspects(Like I used to not appreciate in Java/Spring) and No Magic Interceptors (Like used to not appreciate in Ruby).  One Interesting fact about Rust is that we can do a For in any range interval by doing from 0..100 for instance but we cannot do the reverse like 100..0 so in order to that work we need to do (0..100).rev().

So that's it for today, I hope you guys like it.

Cheers,
Diego Pacheco


Popular posts from this blog

C Unit Testing with Check

Having fun with Zig Language

HMAC in Java