Rust: The Development Environment

Posted on Fri 24 June 2016 in technology

This is the first in a series of articles examining the basics of the Rust language; it is not intended to be comprehensive, and is more than likely to be a pale immitation of the Rust Book which I heartily encourage you to read before using Rust in anger.

Installing Rust

Download and install Rust directly using the following command:

curl -sSf https://static.rust-lang.org/rustup.sh | sh

This will install, among other things, the Rust compiler rustc, and the Rust package manager cargo.

Hello World!

Here is everyone's favorite introductory program in Rust:

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

Pop that into a file called main.rs and compile using the following command:

rustc main.rs

A list on your directory will show an executable "main" in your home directory:

edgeofumbra% ls -l
-rwxr-xr-x  1 inara  staff  345684 Jun 23 19:03 main
-rw-r--r--  1 inara  staff    1262 Jun 23 19:01 main.rs

Execute the binary:

edgeofumbra% ./main
Hello world!

Congratulations, you have run your first Rust program.

Shipping With Cargo

Instead of compiling your programs with rustc, it makes much more sense to use cargo to manipulate your project.

Set up a new application:

cargo new app-market --bin

This creates the following directory structure for you:

app-market
|____.gitignore
|____Cargo.toml
|____src
| |____main.rs

Nifty. Toss the "Hello World!" code from above into main.rs, and try this:

cargo run

You'll see the following happen:

edgeofumbra% cargo run
   Compiling app-market v0.1.0 (file:///Users/inara/src/tmp/app-market)
     Running `target/debug/app-market`
Hello, world!

This is way better than invoking rustc and running binaries manually.

There are a couple of other cargo commands that I find myself using frequently:

  • cargo build will build, but not run the contents of your project.
  • cargo test will build your project and run any tests you have defined for your project.

cargo is very ergonomic: everything works predictably and without surprises. It is totally worth digging around and getting familiar with it.

Dependencies

Cargo.toml is used by by cargo to manage project version information and dependencies. Go ahead and modify it to add a project dependency on the rand module:

[package]
name = "app-market"
version = "0.1.0"
authors = ["Kamil Choudhury <kamil.choudhury@anserinae.net>"]

[dependencies]
rand="*"

The * indicates that we want to pull in the latest version of the rand module from crates.io. I won't go into it here, but you can use semantic versioning to pin the app build to a specific version of the dependency.

Go ahead and build the program:

edgeofumbra% cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling libc v0.2.12
   Compiling rand v0.3.14
   Compiling app-market v0.1.0 (file:///Users/inara/src/tmp/app-market)

Nice. If you have spent any amount of time playing around with autoconf and its ilk, this is definitely a step up.

Autocompletion

If you are planning on using Rust for larger projects, the RACER utility is great for Sublime Text 3 autocompletion. First up, install RACER using cargo:

cargo install racer

Verify that racer was installed and can be found by your shell; you may need to add the .cargo directory to your PATH for this to work:

edgeofumbra% which racer
/Users/inara/.cargo/bin/racer

Install the rust source to a convenient location:

mkdir -p /path/to
git clone https://github.com/rust-lang/rust /path/to

Next, use ST3 Package Control to install the RustAutoComplete package, and edit the package preferences to point to the RACER binary and the newly installed Rust source. In my case, the preferences file ended up looking like this:

// Copy this and place into your Packages/User directory.
{
    // The full path to the racer binary. If racer is already
    // in your system path, then this default will be fine.
    "racer": "/Users/inara/.cargo/bin/racer",

    // A list of search paths. This should generally just
    // be the path to the rust compiler src/ directory.
    "search_paths": [
        "/path/to/rust/src"
    ]
}

At this point autocompletion options for the Rust standard library should show up when editing .rs files. You can add to the search_paths parameter to enable autocompletion for other, non-core libraries.

Conclusion

This post has shown you how to set up a basic Rust development environment. Next up: the Rust type system.