My Introduction to Rust
My experience learning and building something with Rust, from someone new to a low(er) level, systems, programming language.
Objective
Having only worked with higher level language throughout the course of my education and career (Python, TypeScript, Java, C#), I wanted to better understand what a lower level language was all about. I have heard so many good (and bad) things about the Rust programming language, so it seemed like as good a place as any to get started. It seems like every other week I see another organization or project moving code over to Rust, so there had to be a reason for the hype. Having said that, there are also many articles about projects or individuals trying to use Rust and getting burned by it. To be honest, I will not be able to tell you if Rust is overhyped or not. However, I will be able to share a little about the language and what I learned about during my experience of learning and building something with the Rust programming language.
This post is a follow up to a presentation I gave to the Sioux Falls Developers Group on July 11th, 2024. To see the code of the project I built as well as the presentation, here is the GitHub repo: https://github.com/tlarnold10/rustPresentation
What is Rust
I will keep this section to a minimum as there are hundreds of articles, videos, and websites dedicated to talking about what Rust is and its pros and cons. Rust is a low(er) level programming language that provides the developer the ability to manage memory in their programs by themselves. This differs from "higher" level languages that manages memory for you, usually via something called the garbage collector. There are many debates about whether Rust should belong in this classification of a low level language, but for the sake of simplicity, I am just going to consider it a low level language. Rust is owned and managed by the Rust Foundation, with supports from pretty much every major tech company, AWS, Google, Microsoft, and Meta to name a few. Is often seen as a systems level programming language because of its comparison to C and C++, though many people have used rust for non-system level applications.
Why Rust?
Below are a few reasons why you would want to use Rust in 2024.
Extremely fast and reliable
Statically and strongly typed language
Most admired language from the 2023 Stack Overflow survey, and pretty high on the desired rank. https://survey.stackoverflow.co/2023/#section-admired-and-desired-programming-scripting-and-markup-languages
Gaining traction with an active community. As I mentioned above, I see articles on a regular basis of groups adopting Rust. Having said that, not everyone agrees that these changes are good things. More on that below.
Great cross platform compatibility
Why Not Rust?
Now that we have covered why you should use Rust, let's go over why you should not use it.
Several difficult concepts to learn, especially if you are coming from higher level or dynamically typed programming languages. I cannot speak about what it is like to go from C or C++ to Rust, but some people think it is easier, while others think it is more difficult.
Lower developer velocity (takes longer to actually build and ship things).
I have talked with a developer who codes in Rust daily and he disagrees with this statement. His argument is that it might take longer to get an initial solution built, but iterating on that base solution is much quicker because of the safeguards that Rust has in place. The way Rust handles errors, for example, makes trouble shooting bugs in the future much easier.
Not for UI related application development (native and web).
Once again, this could be challenged. Rust does have support for WebAssembly, but the effectiveness of web development with Rust and WebAssembly is not something I am well versed in.
Very few job openings as many organizations are interested in the language, but have not yet found a place for it.
"Rust developer" search on Indeed resulted in 500+ jobs, while "Java developer" resulted in 10,000+ jobs. Having said that, Java is 17 years older than Rust, so there is more code in the wild written in Java that needs to be maintained.
While it is very fast, that is not really needed for a majority of application development, usually you would not notice the performance improvement.
What I learned
As mentioned above, I was able to actually build something in Rust, a mad-libs type word game. The code and ReadMe file can be found here with details on how to run it: https://github.com/tlarnold10/rustPresentation. This was a nice project because I had to come up with the code on my own, not following an existing tutorial, and able to utilize several difficult concepts (reading from a file, borrowing, traits, HashMaps, enums, structs, and Strings). Here are some things I learned from this experience:
Memory management is difficult and is another concept to keep in mind as you are developing. I had to read through several articles multiple times for the concept of ownership and borrowing to finally click, and even still it takes me some time to recall how it works.
Rust's approach: https://doc.rust-lang.org/1.8.0/book/references-and-borrowing.html
Pretty good resources out there, but they vary in quality. Seems like so many different ways to do a certain thing, which is very strange because Rust is very opinionated in how to code in Rust. I will touch on this later, but the compiler provides a lot of code formatting suggestions, some of which I was not a fan of.
Good
Compiler catching many issues and (usually) provides great recommendations to fix.
Learning was actually much easier than I thought it would be
I read through most of The Rust Programming Language, which was a really well written book.
Bad
- Painfully long compile times and, for a program that used a single `main.rs` code file, it would take tens of seconds to compile with each change. If you are new to a language and have to do a lot of trial and error of certain syntax and concepts, this gets to be a long time when you are starting out.
Compiler always had some kind of code style suggestion, some of which I really do not like
For example it suggested that I remove the parenthesis in my if statements
Variable not using `snake_case` will result in an error, which drove me insane as someone who prefers `camelCase`
Ugly
Difference between `str` and `String` data types! Similar to Rust's ownership and borrowing concepts, this took me so long to understand... I had to write out the difference between `String`, `&str`, and `&String` from reading several different articles for it to finally make sense. If you were coding in Rust on a regular basis, you certainly could get use to this, but as someone who just spent a few hours a week on it, I would keep getting hung up working on strings in my application.
Personal preference: I don't think the code looks as "clean" as others. This is very subjective because other people think it looks great
Also, `snake_case` just looks ugly to me and I don't like to keep hitting the `_` character...
What next?
What would I have learned next?
Really need to better understand the difference between `String` and `&str`... during the presentation to the Sioux Falls Devs group, definitely something that I had difficulties explaining.
Lifetimes are a big part of Rust and get much more difficult to understand
Dig deeper into how the borrow checker works. I think I could do this is I continued to work with the language on a larger project.
Could continue with the current project by implementing a "server" and a "client" side of the application, especially when trying to implement multi-threading. It might also be helpful to learn a little WebAssembly as well.
Besides embedded systems, the SF Devs that attended the presentation thought that WebAssembly could be another good use case to pick up WebAssembly. There is a note that comes with that though... the number of production systems currently using WebAssembly is fairly large
Conclusion
Would I recommend Rust? Sure! Keep in mind that there are not a ton of openings and many companies are trying to understand the actual use case for the language... I don't think Rust is going to decrease in popularity, but if you are looking to land a good job because you know Rust, it might not be super easy. The language has tons of promise, so worth picking up if you have not used a lower level language. It ultimately comes down to your career aspirations and where you are in your software development journey.