Rust is, in a word, beautiful. If I was allowed to use multiple words, I'd probably add explicit, safe, well-designed and multi-paradigm into the mix. The language provides immense freedom, but at times it becomes a bit too liberating.
You'd be restricting yourself if you simply thought of Rust as a better C, even though it is. Rust provides abstractions when you really need them, unlike C. There's a thoughtfully designed type system (which does get in your way at times, but the benefits far outweigh the costs). But again, you'd be restricting yourself if you thought of Rust as a less abstract Haskell with no GC, even though it is. You don't have to worry about monads or functors or their "purity".
Rust is multi-paradigm, meaning you (the programmer) have complete freedom to choose the best paradigm for the task. It is a crazy mishmash of the best features of many languages, and yet it manages to be so simple that everything is an expression with an elegant, consistent syntax. This means that you can write this (reading the if
and else
as similar to C):
fn greater(a: i64, b: i64) -> i64 {
if a > b {
return a;
} else {
return b;
}
}
or this (similar to a ternary operator, no explicit return, the last expression in a function is considered to be the return):
fn greater(a: i64, b: i64) -> i64 {
if a > b { a } else { b }
}
A casual reader might not appreciate the difference between the two. return a
is a expression. But adding the semicolon makes it a statement. Specifically, it makes it an ExpressionStatement. To make this clearer, take a look at this example, which is also valid Rust:
fn greater(a: i64, b: i64) -> i64 {
return if a > b { a } else { b };
}
Of course, the second example is considered "idiomatic Rust", and I'm slowly coming to actually understand what that means.
I've been working on a small project of mine (~600 LoC) which is a terminal app that provides a good front-end for the Nozbe project management tool. My code is still far from idiomatic, and not very readable. But I've learned and tried to make it so. Rust has its own unique dialect with best practices and no-nos. Here's what I've found helpful (as a beginner) for navigating this space and writing idiomatic code:
-
Find popular crates that have similar tags and categories as yours and go through the source, seeing how problems and patterns are solved
-
The Rust tag on Github usually has excellent examples of good code quality
-
Check out the official sources: the Rust book, the
std
implementations and docs set the standard to aspire towards -
Rust by example is such a good resource that I have to mention it separately
-
Prioritize code that is elegant and concise
-
The
Result
andOption
types are your friends, use them everywhere
There's a lot of beauty in Rust, and as a language, it enables programmers to express their thoughts in the most elegant way possible. It is outstanding in the faith it has in programmers and the freedom that it provides (at zero cost). I hope that this article helps you in expressing your own thoughts better.
Good luck.