Getting Started with the Rust Programming Language

Getting Started with the Rust Programming Language

Rustacean Banner

Getting Started with the Rust Programming Language

A programming language that has begun picking up momentum in the community lately is Rust. This is a guide aimed at helping folks get started with the Rust Programming Language. Rust has been around for a while with a stable v1.0 shipping in 2015. Rust initially came from Mozilla who maintain the multi-million line C++ codebase in Firefox that has bugs that Mozilla devs speak openly about being unfixable due to the unintended consequences up and down stream as a result. Rust does an amazing job via memory ownership and strict compilation rules, that address the massive problem of memory safety bugs and CVEs. In short, Rust is the overbearing parent you need in your life.

In my (fairly new to Rust) opinion, Rust opens up lower level systems programming with performance on par with C/C++ to a wider audience. As a card carrying networking community member, I think there is potential for those who have been exploring Python, Java, Go to go a bit deeper into the breach with Rust. Be forewarned, it is a lower level language and thus is inherently more complex due to less abstractions.

Oh, did I mention it actually has a dependance and build management system built in! I love Go but it’s dependency management is a dumpster fire, at last count there are 16 separate project build/dependency management. Pip/virtualenv is well, Python being Python and I still suffer nightmares of 45 minute Maven builds from my Java days.

Rust Language Highlights

If you learn like I do, here are the highlights and then skip to the bottom of the page for resource links that I have been using the past few weeks to start learning Rust. These are my general impressions of the language after a few weeks of hacking:

  • Performance on par with C/C++ but much safer due to the unique cornerstone of Rust memory ownership and borrowing for safety guarantees without implementing a garbage collector.
  • An amazing (and simple to use) built in dependency and build management system.
  • Lots of examples in the std library API docs. The syntax can get a bit harry here and there so having examples in the docs are great
  • There is no concept of nil values. Since nil/null can result in null pointer exceptions or worse, a dev never doing null checks, in Rust output can have either (Some) value or no value (None) and for results either success (Ok) or failure (Err). This is similar to Haskell with match statements. This has probably been the hardest thing for me to get used to. Some(T) is the closest you get to a null value in Rust.
  • Compiles to a binary.
  • String vs str is kind of clunky. A String is essentially heap allocated and growable that you generally would use if you need to modify the data (same as Vec). A str is a fixed length view of the data that you would borrow using a reference &str.
  • Functions only return one value (denoted with a -> operator). They do not require a return statement but can perform early returns with a return statement. Get used to the enum Result< T, E >.
  • Conveniences of a high level language in a low level language that lowers the barrier to entry of this type of systems programming (Rust supports methods!).
  • Built-in support for multi-threading
  • Strict compiler and rules in general (memory ownership) that protect you from yourself.
  • Rust is positioned to be the leader in WebAssembly apps due to its extremely small runtime footprint like C/C++ but with tooling that makes it attractive (imo).
  • You get to watch C++ programmer heads explode when you ask them about Rust.

Dealing with Memory in Rust

One of the biggest difference from other languages is there is no garbage collector, nor is there explicit memory management. Rust doesn’t let you run with scissors and in turn makes it pretty easy produce stable code due to the strict nature of ownership. In lieu of garbage collection is the concept of ownership.

The following are the three rules of ownership as defined by the Rust Book distributed with the Rust releases. I added a couple of thoughts to them (not in bold) below.

  • Each value in Rust has a variable that’s called its owner. – the borrow checker can be painfully consistent (but that’s a good thing). The alternative is a 10,000 line method where nothing transfers since you borrowed everything at once.
  • There can only be one owner at a time. – If there were two owners at once, and changes the ptr the other has no idea. This prevents the double-free scenario in C or C++.
  • When the owner goes out of scope, the value will be dropped. – The easiest example is passing a variables to a function. The variable gets moved into a new stack frame and the old one is destroyed in the stack. You can work around this by referencing immutable borrowed references (e.g. &x).

Source – The Rust Way of OS Development



Source – A proactive approach to more secure code

Rust Concurrency

Like any new language built by the community is like a big boat, it can be hard to steer. Concurrency is one of the harder problems in computing, so it’s no surprise there are a number of various runtimes to deal with concurrency. Since most people reading this care about networking, a concurrency implementation will be relevant for async network i/o. The majority Rust projects I have run across use tokio but as of November, 2019 async-std v1.0 stable which future APIs will be built on this initial release. Rust natively uses OS threads. Rust had a green thread implementation prior to v1.0 that was removed. Tokio and possibly the newer async-std look to address the c10k issue of spawning a million threads for the OS to manage. I’m guessing futures/async will be a choose your own adventure area for a while.

Getting Started Hacking with Rust

I think the basic getting started exemplifies the extent the creators went to in empathizing with a user in making an incredibly easy starter project.

Current installation instruction for Rust and it’s toolchain can be found at rust-lang.org

Currently, you can use:
curl https://sh.rustup.rs -sSf | sh

Next, you can create a quick project to validate the installation and run your first project:

This will create a hello world sample in src/main.rs that contains a main function with a print statement.

To compile your app to a binary, you can do something like this:

Install an IDE for Rust

For an IDE, there are two main ones. VSCode and the Jetbrains Rust plugin. Jetbrains tends to open source a new language for development and bug testing and then discontinue the project when they commercialize the platform, e.g. Goland. I’ve tried both and prefer VSCode. If you choose VScode, once it is installed, go to preferences -> extensions and install Rust Language Server (rls). As a consumer of Jetbrains products for a long time, this has converted me to VSCode for Rust dev. I still love Intellij for Java fwiw.

Rust Learning Resources

Lastly, below are the resources I found helpful for getting started.

That’s it for now. Thanks for reading. I will be posting more in coming weeks including how Rust can impact the networking landscape we have today.

About the Author

Brent SalisburyI have over 20 years of experience wearing various hats from, network engineer, architect, ops and software engineer. More at Brent's LinkedInView all posts by Brent Salisbury →