I Love Rust
By that I mean Rust the programming language, not Rust the videogame; sorry to anyone coming here thinking this post was about that.
I've been trying to learn Rust the past few months, and I think I'm finally getting a grasp of it---at least to some extent---and as of late, I feel like the language has started to click into place for me.
For those who don't know, I have a pretty diverse theoretical background in programming. In 2012 I learned C# at a "profession academy", in 2013 I learned Scala and Haskell out of my own curiosity, in 2014 I learned ANSI C (aka C89) as part of an imperative programming course at the university, and in 2017 I dabbled with some C++ as part of an embedded programming project.
In a sense, my interests have always been a mix of procedural and functional code (functional in the sense of FP, not in the sense that the code works). When I write C#, I go out of my way to make the code as functional as possible, relying heavily on delegates (C#'s closures) and Linq to bend the various collection types to my will. One could argue that this functional approach to C# comes as a result of my intense love for Scala, which marries FP with OOP in an interesting natural way.
In comes Rust. I would mostly describe the language as a weird merger between C++ and Scala, in fact, I'm surprised more people don't make the syntactic connection between the languages; take this example:
Scala
def safeDivide(a: Double, b: Double): Option[Double] = { return if(b != 0) Some(a / b) else None }
Rust
fn safe_divide(a: f64, b: f64) -> Option<f64> { if b != 0.0 { Some(a / b) } else { None } }
Apart from the different name for a 64-bit floating point number, and the explicit curly braces in if-statements, the languages are damn near identical in a lot of ways, even down to their use of Associated Types (called Abstract Types in Scala).
It is however important to note that Rust---unlike Scala---is not object oriented, even though it does use a few OOP ideas, the language itself is almost entirely procedural + functional, whereas Scala is object-oriented + functional.
Rust has no classes and no inheritance, but does instead have composition through traits, which are interface-like constructs with a few more built-in features. Traits let you extend existing types at will, and they even let you implement default behaviour similar to abstract classes.
The weird thing is that I feel at home in Rust---just like I do in Scala. Even though Rust is all about explicit programming, foregoing implicit conversions in favour of having the user type everything out, where Scala is quite literally the opposite, I still feel at home. It has that feeling of C's low-levelness which I actually really like. It also has all the cool constructs I love from Haskell. But unlike Haskell---as much as I love it---I feel like I actually get work done in it. It feels like a workhorse language, just like the C family of languages.
Just the other day I implemented a word generator for a constructed language I'm making, which was capable of taking in a sequence of rules, spitting out a string of letters, and I did it in like 56 lines of code. When that compiled, when it ran without a hitch, and worked exactly how I envisioned it I was completely blown away. I did that in less than 100 lines of code?! Doing something simiar in C# would require 4 times that; not because C# is a weak language by any means, but because of the sheer number of classes and files and ritualistic public static blah blah blah you need just to get any code running. Naturally, the current version of the code is a bit longer; somewhere around 74 lines of code, but that's due to the fact that I added a bit more functionality, including a few macros that make the writing of rule sequences incredibly painless and intuitive.
I think this code was when I realised Rust had clicked for me... At least a little bit. I still have troubles with it; some of the things about the language are a bit confusing to me still, but that's natural with any language, especially one with a learning curve as steep as Rust's---and steep it is.
Like C and C++, Rust does not have or use a runtime, and thus does not have a garbage collector. Sounds really annoying to work with, but actually not! For the most part, Rust's compiler does all the memory de-allocation for you before the code even runs! Following a set of very strict rules, Rust manages to know exactly when and where in a program memory needs to be de-allocated, so you don't have to do it yourself. This does of course mean you need to know exactly what you're doing, or Rust will tell you you're doing it wrong. "Fighting the borrow checker" is a common newbie issue in Rust, and for good reason, you basically have to throw every habit you have out the window, especially if you're coming from a language with a garbage collector.
That said, I still love it! My code might not be up to par with the best rustacians (the Rust-community word for a Rust-programmer), but it's slowly getting there.
If you're interested in the word generator I wrote, you can find it here: https://github.com/ElectricCoffee/word_generator















