Development

Tutorial: Installing Rust

23 Jul 2019 12:00pm, by

Last month I got a chance to pose some questions to the creator of Rust. But he’s not the only person thinking about Rust improving the security (and memory-safety) of our code. In 2017 TechCrunch urged engineers refactoring C code to begin replacing it with Rust, and the same year even open source guru Eric S. Raymond began hailing Rust as a plausible successor to C, as did Joyent Chief Technology Officer Byran Cantrill. And just last week Microsoft announced it was also exploring Rust as an alternative to C, as well as to C++.

So it seemed like high time that I got Rust up and running on my own system — to see just how easy (or how hard) it can be to install and run.

And to learn what I could about the unique advantages of Rust code…

Rust without Installation

The first thing I learned is that the Rust ecosystem is surprisingly robust — and that it’s possible to run Rust code without even installing anything. There’s a browser-based online “Rust Playground” where you can mess around with the language yourself. It starts you off with a pre-written “Hello world!” program, and dares you to take it from there.

 Hello World at Rust playground

That page features a lot of bells and whistles. The three dots on the far right pull up a menu that lets you select which Rust to run — either the earlier 2015 edition, or the 2018 edition — and whether or not you want to enable a debugging backtrace when it executes to give you even more visibility into how the program runs. There’s also a down-pointing arrow which brings up a menu for choosing which version of Rust to run — the current stable version, or the “beta” version (a more-recent version which gets updated every six weeks, according to the official Rust blog). And in addition, every 24 hours the last successful new build becomes the new “nightly” build, to “give early adopters access to unfinished features as we work on them.” This is also available through the same menu (via that down-pointing arrow).

But even the playground’s “Run” option has several choices. One choice lets you compile your code without running it — with or without testing it for errors — or to convert it to Assembly or to WebAssembly. And other choices let you convert your code to MIR (Rust’s “Mid-level Intermediate Representation”) or to LLVM IR (the intermediate representation for the Low-Level Virtual Machine that Rust uses as its backend).

It’s fun to see how their simple “Hello World” program expands with each selection.

Some Hello World code in Assembly

But of course you can also create your own local installation of Rust, and Rust has its own installer/version management tool — called rustup. It’s available at Rust-lang.org on a slick web page which helpfully detects your operating system, and suggests the appropriate download.

Although if you access the page using the command-line browser Lynx in the Windows Subsystem for Linux, it apparently gets very confused, and displays messages for three different operating systems…

Rust install instructions via Lynx

rustup is officially described as a “toolchain multiplexer” that installs several of the important Rust tools. For example, rustup comes in handy when it’s time to update to a newer version of Rust. Just type:

Rustup’s official documentation calls this “the essence of rustup”. But another neat thing about rustup is it makes it easy for you to manually switch between those three versions of the Rust compiler — the stable, beta, and nightly versions.

And if you want to make sure your rustup tool is up to date, you type…

So it seems deceptively simple to install Rust on a Windows system. You just download and run a file named Rustup-Init.exe

But of course, it wasn’t that easy…

Installation and a Detour

rustup checks for the necessary prerequisites, and lets you know if anything’s missing. In my case, I also needed to install the Microsoft C++ Build Tools — but Rustup-Init.exe told me I could get them by installing Microsoft’s Visual Studio IDE and then making sure that it also installed the C++ build tools — and even gave me the appropriate URL.

Rust install wants Microsoft C plus plus build tools
You can select the installation of build tools from Visual Studio’s workspace screen — although downloading them takes several long minutes…

The Visual Studio Installer alone was 67 megabytes, and Visual Studio Build Tools 2019 required 1.15GB more on my laptop — and took nearly 12 minutes to install. (It seemed to stall at page 83 of 89 pages installed — 35% of the status bar — and then suddenly shot up to 96%.)

Installing build tools with Visual Studio Installe

And when it was finished, a reboot was required.

But one reboot later, and I was running rustup

Getting Started With Cargo

The second thing I learned about Rust is there’s a couple of parts to it — leading to one more very important part of the installation process. rustup also installs the Cargo tool, which will launch the compiler for your code, run your code, and serve as a handy package manager for importing modules of pre-written Rust code — which will make everything easier. (And according to Rust’s official “Getting Started” page, Cargo also makes it easier to test your code, and to write documentation for it.)

For the Windows version, rustup also adds the corresponding commands to your PATH environment variables — rustup, cargo, and rustc (the compiler).

Launching a command window, I ran the compiler’s --version command to prove that yes, it’s been installed. And there it was!

Rust version check

But if I’ve learned anything as a programmer, it’s that it’s never simple to write a “hello world” program in a compiled language. With Rust, first you have to create a new hello_world package (according to the documentation in Rust’s official Cargo Book.) It has a chapter titled “First Steps with Cargo.”

This can be done easily with a single Cargo command…

Creating that package means creating a directory named hello_world with a manifest file (named Cargo.toml) which holds key-value pairs for the name, version, author, and edition of my humble “hello world” program.

making hello_world package

When running the compiler, it will look for a Cargo.toml file in your current directory to tell it which program to compile. (And it will stop with an error if the file isn’t found.) You don’t even have to specify the name of the program — it’s in the manifest. So just being in the appropriate directory is enough. You just type cargo build.

building hello world (cropped).

That cargo new command also created a separate src subdirectory where the actual code will go. I thought I was going to have to type in some lines of code (to print out “hello world” on my screen) — but apparently not. Cargo created a main.rs file in my src directory which already contains an entire main function — and which already includes the appropriate print statement.

fn main() {
println!("Hello, world!");
}

Did Cargo guess that since I was making a package named “hello_world,” I must want it to print out “hello world”?

No. It’s apparently the default stub that appears in every src directory’s main file. I tested this by creating a package named goodbye_world — and Cargo still included the exact same code in the main file it created. (Including a println statement that prints out “Hello, world!”)

So it’s a feature, not a bug — and the pre-written code now stares tantalizingly from my hard drive, daring me to finally compile it into my first executable Rust program. (Or, as the Cargo book explains, “Cargo generated a ‘hello world’ for us. Let’s compile it.”)

A Surprise Ending

Er, and unfortunately, here’s where the Cargo Book failed me. It told me that to run the program, I simply needed to type:

Nope.

Rust error - not recognized

Fortunately, the Cargo Book also provides an alternate (and simpler) way of running Rust programs: just type cargo run in the appropriate directory — which will compile and run your program, all in one glorious step.

Hello World run from Rust on a Windows command line

It was glorious.

I did the building-and-running command a few more times, just because I could, just to prove to myself that it was really working.

And to savor the fact that I was finally running Rust my very first program…

Feature Image by meisjedevos from Pixabay.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.