Rust - A Deeper Look
Prelude
I’m mainly a Java developer, I also write react/htmx web on the side but not that much. However I keep wondering, why other language exists? Why every month a new language seem to pop-up here and there to do something basically (from my point of view) Java also can do? What is so good about other language?
I want to know, and the best why to do it is just to simply start writing with it. So I start to move, every week, a new language installed and ready to use. This ‘series’ is basically my review/note on how that language feel
Rust?
Rust is basically advertised as new thread-safe way to build a low level application. My first exposure to rust is basically the ‘Rewrite it to Rust’ meme.
Back then I don’t really know what is Rust, I only knew that cpp is the ultimate and hardest language, while Rust is like the newer more safer thing?
After learning how rust work, and with additional information came from working with another language. I start to see what hole Rust is filling.
When Performance is Everything
When talking about performance, cpp was said to be the ultimate tool. It is mainly came from the fact that we can directly control how variable is passed. Someone who learn deeply about Computer and not just simply learning about how to write code will definitely knew how to take this advantage. Rust allow us to do the same thing with cpp but in a more safer way using concept of borrow-checker. Just like cpp, rust also has something like * and &, however it has different usage compared to the one on cpp. It still used to passed on memory address thou.
We are also forced to a new paradigm where we must prefer immutable variable first. Even when we had to edit thing, we are encouraged to just pass the variable to the new function and not clone the variable because that take work!
Rust doesn’t have garbage collector, instead it rely on this borrow-checker concept. It mean that Rust will immediately remove any variable that doesn’t hold pointer to any used memory.
Once a variable ‘give’ it address to other variable, then it will lost it and will be thrown away.
Note that this borrow checker concept will only truly appear when used on non-primitive variable. If it a primitive variable then it will just do a usual Copy the bit mechanism
let abc: u8 = 1;
let b: u8 = abc; // value of abc is copied to b
let c: u8 = abc; // and then copied to c, since only have to copy the bit is cheap
When working with non-primitive this will happen (for example a String, but later we are truly going to feel this when working with struct)
fn printStuff(a: String) {
println!("{}", a);
}
let abc: String = String::from("original string");
printStuff(abc); // Value of abc is given to printStuff, abc no longer valid after this
let want_something: String = abc; // This will not compile, abc has been removed sinve the value has been passed.
However, the safeguard that is imposed by Rust may hinder other expert programmer who used to work with cpp. There are some techniques that can be considered safe (because they know it will be safe based on their business requirement or else), but Rust will considered it unsafe.
Maybe its like riding a train on a rail at the very edge of a cliff, you know its safe but it doesn’t look safe from others view.
So if you knew better, then Rust might not be your ideal language.
However, if you work on organization with varrying level of knowledge, then this will give the senior leadership who can’t read every single line written by its junior a safe assurance. At least the junior can’t topple the train into the abyss because they forget a *.
World-class Builder
Working with cpp make me realize that older language doesn’t have luxury of easy package and builder. Cpp require me to install gcc (and its complex) meanwhile rust, its already a complete package.
Cargo (the build tool) also give a good error message to let me know what wrong, if you came from javascript this will be a good experience.
The fact they also provide a single official repository is also a god-send (crates.io). I used to work with Maven and honestly I take that as granted, but as soon as I see how zig handle its library distribution, I immediatelly understand how helpful having an official repository combined with its build tool is.
For those who dont know, zig doesnt have an official repository, so the distribution is a community effort. The distribution itself is not simple since the distribution is more like a catalog of product where we can go to github of the library and add it based on guide from the creator
When to Use
So basically, why does Rust exists?
it give us a modern way to write a low-level application.
Its thread-safety is often advertised as something that will be the win point against cpp.
I learn this to make sure that I have option when I need to write a low level application. Example, I may need a simple tcp connection to stream data fast and I don’t want to add complexity of whole SSE to the stack. Rust give me way to do that without relying to other library. Its nice to know that we have a way to do it now, all we need to do is understand and learning the basic.
Coming from Java and Spring Boot, it is an eye opening experience for me that I can create something that doesn’t have depend on other language. I also can appreciate go language and htmx more because of this.
Note
Gleamis written in RustLuacan be used with Rust to give a better business expression to the language without having to rely on more complex Rust language.- I tried to do a calculation that basically do 25000 iteration and it finish it in 0.05 second.
Some basic common command
cargo new new_project
cargo run