Serdar Yegulalp
Senior Writer

Rust tutorial: Get started with the Rust language

how-to
Nov 21, 202213 mins

Here's how to get your feet wet with Rust, from using Rustโ€™s toolchain to creating projects, working with third-party code, managing libraries, and more.

Rust language
Credit: GagoDesign / Shutterstock

Over the last few years, Rust has evolved from a curiosity brewed up in a Mozilla employeeโ€™s lab to a strong contender for the next generation of native applications and bare-metal solutions. Those advances come from Rust providing its own toolchain and component management systemโ€”along with certain popular features and quirks.

This article is for developers new to Rust, or considering it for future projects. Weโ€™ll walk through setting up a working environment in Rust, configuring an IDE, and making the most of Rustโ€™s excellent application development toolset.

Understanding Rust releases

Rustโ€™s toolchain consists primarily of the Rust compiler, rustc, along with tools for managing a Rust installation. Because Rust is under constant development, itsย toolchain is designed to be easy to update.

Software projects are often provided via multiple channels in order to separate the stable and beta versions of the code. Rust works the same way, offering three channels for toolchain updates:

  • Stable: Major point releases, which emerge every six weeks or so.
  • Beta: Candidates for the next major point release, which emerge more often.
  • Nightly: The most immediate build, with access to cutting-edge features but no guarantees as to their stability.

As developer Karol Kuczmarski has pointed out, itโ€™s best to think of the nightly Rust channel as its own language. Some Rust features are only available in the nightly channel, and they can only be activated by special compiler directives. In other words, they wonโ€™t even compile on the beta or stable channels.ย 

Thatโ€™s by design, because thereโ€™s no guarantee the nightly features will be supported anywhere else.ย However, many of those features eventually graduate out of the nightly channel and into beta and stable releases. (Compiling to WebAssembly, for instance, works in the stable version as of Rust 1.30.)

What does this mean for you as a developer? In short:

  1. Use stable for actual production work.
  2. Use beta to test current software against upcoming versions to see if anything may break in the upgrade.
  3. Only use nightly for sandboxed experiments with Rustโ€™s newest features.

Choose an OS for Rust development

Rust supports all three major platformsโ€”Windows, Linux, and macOSโ€”in both 32- and 64-bit incarnations, with official binaries for each. A slew of other platforms also have official binaries, but they donโ€™t have the same level of automated test coverage. These second-class platforms include ARMv6 and ARMv7 for iOS, Android, and Linux; MIPS Linux and MIPS64 Linux; 32-bit editions of x86 iOS, Windows, and Linux; and WebAssembly. Other platforms, like Windows XP or the experimental HaikuOS, are supported through unofficial builds.

Rustโ€™s development team has stated that being broadly portable isnโ€™t one of Rustโ€™s missions. For example, although Rust is available on many ARM architectures, there is no guarantee that Rust will be officially supported on low-end hardware platforms.

That said, there should be a supported Rust build available for the vast majority of common, mainstream use casesโ€”namely, 32- and 64-bit Windows, Linux, and macOS.

If youโ€™re planning to develop in Rust on Windows, keep your toolchains in mind. Rust supports two Windows toolchains:

  • The native Microsoft Visual C (MSVC) ABI
  • The Gnu ABI used by the GCC linker

Because almost all C/C++ software built in Windows uses MSVC anyway, youโ€™ll want to use the MSVC toolchain most of the time. If you ever need GCC, itโ€™ll most likely be for interoperating with third-party libraries built in Windows with GCC.

The good news is that Rustโ€™s toolchain management system lets you keep both MSVC and GCC tool chains installed, and it lets you switch between them on a project-by-project basis.

One of Rustโ€™s compilation targets is WebAssembly, meaning you can write in Rust and deploy to a web browser. WebAssembly itself is still rough around the edges, and so is Rustโ€™s support for it. But if youโ€™re ambitious and you want to get your hands messy, read this book, which details the process for compiling WebAssembly to Rust. Written by Rust and WebAssembly engineers, the book includes a tutorial for an implementation of Conwayโ€™s Game of Life that is written in Rust and deployed as WebAssembly.

Start your Rust setup with rustup

Rust provides an all-in-one installer and toolchain maintenance system called rustup. Download rustup and run it; itโ€™ll obtain the latest versions of the Rust toolchain and install them for you.

The most critical tools maintained by rustup are:

  • rustup itself: Whenever new versions of rustup or other tools are published, you can just run rustup update and have everything updated automatically.
  • rustc: the Rust compiler.
  • Cargo: Rustโ€™s package and workspace manager.

By default, rustup installs Rust from the stable channel. If you want to use beta or nightly versions, you have to install those channels manually (for example,ย by running rustup install nightly) and set Rust to use them by default (rustup default nightly). You can also manually specify which channel to use when compiling a Rust application, so you donโ€™t have to set and reset the default every time you move between projects.

Rust tutorial: rustupย keeps all parts of your Rust toolchain updated. IDG

Figure 1. rustupย keeps all parts of your Rust toolchain updated to their most recent versions. Here, the nightly toolchain, with bleeding-edge and potentially unstable language components, is being updated separately from the stable version.

You can also useย rustupย to install and maintain custom toolchains. These are typically used by unofficial, third-party builds of Rust for unsupported platforms, which usually require their own linkers or other platform-specific tools.

Note that another default assumption Rust makes is that it stores Cargo filesโ€”the downloaded packages and configuration informationโ€”in a subdirectory of your user profile. This isnโ€™t always desirable; sometimes people want that data on another drive, where there is more room, or in a place that is more accessible. If you want Cargo to live somewhere else, you can relocate it manually after the setup is finished. Here are the steps:

  1. Close down all programs that might be using Cargo.
  2. Copy the .cargo directory in your user profile to where you want it to live.
  3. Set the environment variables CARGO_HOME and RUSTUP_HOME to point to the new directory.
  4. Set the PATH to point to the bin subdirectory of the new directory.
  5. Type cargo to ensure Cargo is running properly.

Configure your IDE for Rust

Despite Rust being a relatively new language, itโ€™s already garnered strong support from many common IDEs. Developer Manuel Hoffman maintains a project to track the state of such support at the websiteย areweideyet.com.

Making Rust work well with IDEs is an express goal of its development team, via a feature called the Rust Language Server (RLS). RLS provides live feedback about the code in question from Rustโ€™s own compiler, rather than from a third-party parser.

Rust tutorial: Rustโ€™s Language Server provides compiler feedback to an IDE. IDG

Figure 2. Rustโ€™s Language Server project provides live feedback to an IDE from the Rust compiler for the code youโ€™re working with. Visual Studio Code, shown here, has some of the most complete support available for the Rust Language Server.

Here are the IDEs that support Rust:

Create your first Rust project

Rust projects are meant to have a consistent directory structure, with code and project metadata stored within them in certain ways. Code is stored in a src subdirectory, and details about the project are stored in two files in the projectโ€™s root directory,ย Cargo.toml (the projectโ€™s basic information) and Cargo.lock (an automatically generated list of dependencies). You can create that directory structure and metadata by hand, but itโ€™s easier to use Rustโ€™s own tools to do the job.

Rustโ€™s Cargo toolย manages both Rust projects and the libraries, or โ€œcrates,โ€ they use. To spin up a new Rust project named my_project in its own directory, type cargo new my_project. (For C# developers working with .Net Core, think of the dotnet new command.) The new project appears in a subdirectory with that name, along with a basic project manifestโ€”the Cargo.toml fileโ€”and a stub for the projectโ€™s source code, in a src subdirectory.

When you create a new project, aย main.rs file is automatically created in the projectโ€™s src directory. This file contains a basic โ€œhello worldโ€ application, so you can test out your Rust toolchain right away by compiling and running it.

Here is the source code for that basic โ€œhello worldโ€ application:


fn main() {
ย  ย  println!(โ€œHello World!โ€);
}

To build and run the application, go to the project directoryโ€™s root and type cargo run. Note that by default, Cargo builds projects in debug mode. To run in release mode, use cargo run --release. Binaries are built in the projectโ€™s target/debug or target/release subdirectory, depending on which compilation profile youโ€™re using.

rust compile 03 IDG

Figure 3. When a Rust project is compiled, all its dependencies are obtained and compiled automatically, as well. Detailed line-by-line feedback appears for anything that raises a warning or a full-blown error.

Work with Rust crates

Package management is a key part of any modern programming environment. To that end, Rust provides โ€œcrates,โ€ which are third-party libraries packaged for distribution with Rustโ€™s tools. You can find crates in the official Rust package registry, Crates.io.

If your project has a dependency on a particular crate, you need to specify that crate by editing the projectโ€™s Cargo.toml file. The standard way to do this is manuallyโ€”that is, by simply editing Cargo.toml directly with a text editor. The next time the project is rebuilt, Rust automatically obtains any needed dependencies.

When you build a Rust project that depends on external crates, Cargo looks for those crates on Crates.io by default; you donโ€™t need to obtain them manually. You can also refer to crates in your project by URL rather than by crate name, in case you need a crate that isnโ€™t hosted in the registry, such as something from a private repository.

Note that some crates will only install and build on Rustโ€™s nightly channel, because they use experimental features not available in other channels. If youโ€™re on the release channel and you try to install such a crate, you wonโ€™t get any warning until the compilation itself fails. Crate documentation usually mentions whether it requires the nightly channel or not, so read up before you include, let alone compile.

Crates can come with binaries included. Some are command-line tools used in Rust development; others are general-purpose tools (such asย ripgrep). To install one of these crates, just type cargo install <crate name>. This isnโ€™t the only way to distribute a binary created with Rust, but itโ€™s a convenient way for Rust developers to obtain them as part of a workflow involving Rust tools.

Cross-compile Rust to another platform

Because Rust supports multiple tool chains, even in the same installation of Rust, you can compile Rust applications to a target OS and environment thatโ€™s different from the one youโ€™re compiling on.

Such cross-compiling requires a toolchain on the platform youโ€™re working on that matches the target platform. Sometimes, as with cross-compiling to Linux on Windows, or vice versa, this involves little more than having the GCC linker. But other times, itโ€™s more complex. For cross-compiling to macOS, for example, you need the Xcode IDE libraries to finish the jobโ€”cctools (Appleโ€™s equivalent of binutils) and the macOS SDK.

Third-party tools offer some ways around these difficulties:

  • One such tool isย Cross, which runs directly on a 64-bit x86 Linux host and provides what its creator describes as โ€œzero-setupโ€ cross-compiling to a wide variety of targets, including 64-bit Windows and MIPS.
  • Another tool is Trust, a Travis CI and AppVeyor template that can automatically publish binary releases of a Rust project. Trust can build for Linux, Windows, and macOS, although it requires not only use of the Travis CI and AppVeyor services, but also that your project be hosted on GitHub.
  • The crossbuild project provides a multi-architecture Docker image that can be used to cross-build between all three major platforms.

Note that of these three tools, only Cross has been updated recently.

Work with other Rust projects

A good way to get your legs with Rust is to check out a third-party project and work on it locally. The easy way to do that is just make a Git clone of a Rust projectโ€™s repository. As long as the repo has a Cargo.toml file in its root, itโ€™ll be recognized by Cargo.

One thing Cargo canโ€™t do, at least not yet, is make a local clone of a crate by itself. This is possible with Git, and most anyone doing serious work with Rust should have Git installed anyway. But you can add that functionality to Cargo directly via the third-party Cargo subcommands cargo-clone and cargo-clone-crate.

Where can you find projects to tinker with and learn from? For starters, go to the Awesome Rust repository on GitHub. Among them is the Servo web browser engine project, one of Rustโ€™s first intended real-world applications. Also of interest is the mdBook project, for generating online books and e-books from Markdown documents.

Many other projects are useful on their own merit, not only for getting a leg up with Rust or because theyโ€™re components of a larger effort. These include Trust-DNS (a DNS server and client), Alacritty (a GPU-enhanced terminal emulator), and the MaidSafe decentralized data platform. (For fun, check out Magog, a Rogue-like game.)

Awesome Rust also lists many of the best crates and third-party tools to learn about and put to use.

If youโ€™re hunting for a project to get involved with as a full-blown developer, try searching GitHub for Rust-based projects that have open issues tagged with โ€œgood-first-issue.โ€ That tag indicates an issue that will be relatively easy for a new developer to help out with. Itโ€™s a great way to learn the ropes with Rust while helping out the project.

Serdar Yegulalp

Serdar Yegulalp is a senior writer at InfoWorld. A veteran technology journalist, Serdar has been writing about computers, operating systems, databases, programming, and other information technology topics for 30 years. Before joining InfoWorld in 2013, Serdar wrote for Windows Magazine, InformationWeek, Byte, and a slew of other publications. At InfoWorld, Serdar has covered software development, devops, containerization, machine learning, and artificial intelligence, winning several B2B journalism awards including a 2024 Neal Award and a 2025 Azbee Award for best instructional content and best how-to article, respectively. He currently focuses on software development tools and technologies and major programming languages including Python, Rust, Go, Zig, and Wasm. Tune into his weekly Dev with Serdar videos for programming tips and techniques and close looks at programming libraries and tools.

More from this author