Why Rust?
Here are some reasons why I'd use Rust for some cases, especially backend/server-side. These are purely coming from my opinions.
Type System
It has sophisticated type system. It's so static and strong with additional its Ownership and Borrowing system. This kind of type system will affect the way you design your program, also affect the way you think about the program in the long run. It's immutable by default, and the type system guards the mutability in a way to avoid side-effects or data race. This is known as Aliasing XOR Mutability Principle. This also contributes to API constraints which this post is too short to discuss. Its ownershipt model gives notion of owning a value avoiding passing reference with side-effects. If you want to pass references, its borrow checker will make sure anything passed are safe from side-effects or any other issue related. This is what make Rust safe in term of type, memory and threading.
Rust leaves many technical things to its compiler to handle with its static analysis and error friendly messages. Issues related to type, memory, and thread safety can be caught statically upfront, leaving us to only care about business logic at PR Review.
No Garbage Collection
Its Ownership and Borrowing type system allow it to "clear" memory at compile time, meaning it knows when to release a resource, and release it right away. This is good for long running systems, performance critical systems, and real-time systems. It's part of its zero-cost abstraction mechanism.
Zero-cost Abstraction
A term Borrowed from C++, meaning that you only pay for what you use. The abstractions you added, Rust will as much as it can remove anything irrelevant from lower-level/hand-coded perspective. This will minimize the cost of Readability vs Performance. You can keep your abstractions, and let compiler remove the irrelevants.
Performance
This is obvious seeing from many benchmarks found online. It's in the same league as C and C++.
Efficiency
Benchmark not only showing speed, but also resource utilizations, especially memory. For more or atleast same speed or computations done, Rust tend to have more efficient memory utilizations. There're many languages in this league, C, C++, Go, Zig are some of them, all have efficient memory usages, but to me they lack the type system Rust offers.
Blanket Implementation
It's a generic implementation of a trait for all or some types. This gives you capabilities to extend/custom/overwrites a types functionalities. This mostly usefeul when you have a type with multiple related traits associated eachother. One notable example is conversion trait From<T>
or TryFrom<T>
.
Module System
The code is modular top to bottom. The namespacing are clear at every level. Developing things from small to bigger scale, toolings and libraries give clear separation of concerns. This kind of module system gives better encapsulation capability for modular code. Circular dependencies are allowed in the same crate. The only thing's lacking so far for me is to custom multiple accessibility for a module, e.g. pub(in::crate::module_a, in::crate::module_b, ...)
, for now this is not possible.
Multi-Paradigm
Rust is not specifically Procedural, OOP, nor Functional. It covers all paradigm in its own way. You can have your ways in procedural way of code, or doing things with smart pointers, trait, and trait objects for OOP, or being Functional giving the natures of its type system.
Web/Server Development
After async-await stabilized in 2019, especially after Tokio, one of Rust's async platform stabilized, it gains momentum in web and server development. Many tools/libraries appropriate to be async ported onto Tokio. Async-await abstract away Future type so you can write async code just like you wrote sync code. It's also ranked among top tier in term of performance in many benchmarks. Server/backend development is also one area of where Rust mostly implemented. It's also first choice when doing WASM, even though WASM is not stable yet. JS and NodeJS are still first choice for frontend development.
Multi-Domain
Not only for Web/Server development, it also covers CLI/Repl programs, game development, and low-level/embedded programming. It can speak natively to C, seeing how some of its STDs libs are relying on libc.
Cons
As always there's nothing perfect in this world, I found these are the cons:
Slow and costly compile time
Because of its Type System, it does many things at compile time, ranging from optimizations and code generations, including LLVM optimization. It's also costly it uses all CPU cores for each compilation units for parallel compilations, and also eat memory. It's possible to get OOM at compilation time, instead of runtime.
Steep Learning Curve
You have to fight the compiler each time you tried to compile. One of compilation error frequently happen is borrow checking violations. You reason your program in away not breaking the lifetime of data references. It take time to get used to this.
Not for prototyping
Its obsession for correctness is not suitable for prototyping. Don't spend too much time with compiler if it's only for prototyping.
Productivity
You'll experience slower development in early phase because of strict type system and borrow checker. Where performances and efficiencies don't matter, You don't get much.
There are many other things about Rust, I'll post it another time.