book/2018-edition/src/ch01-01-installation.md

5.2 KiB
Raw Blame History

Installation

The first step is to install Rust. Well download Rust through rustup, a command line tool for managing Rust versions and associated tools. Youll need an internet connection for the download.

Note: If you prefer not to use rustup for some reason, please see the Rust installation page for other options.

The following steps install the latest stable version of the Rust compiler. Rusts stability guarantees ensure that all the examples in the book that compile will continue to compile with newer Rust versions. The output might differ slightly between versions, because Rust often improves error messages and warnings. In other words, any newer, stable version of Rust you install using these steps should work as expected with the content of this book.

Command Line Notation

In this chapter and throughout the book, well show some commands used in the terminal. Lines that you should enter in a terminal all start with $. You dont need to type in the $ character; it indicates the start of each command. Lines that dont start with $ typically show the output of the previous command. Additionally, PowerShell-specific examples will use > rather than $.

Installing rustup on Linux or macOS

If youre using Linux or macOS, open a terminal and enter the following command:

$ curl https://sh.rustup.rs -sSf | sh

The command downloads a script and starts the installation of the rustup tool, which installs the latest stable version of Rust. You might be prompted for your password. If the install is successful, the following line will appear:

Rust is installed now. Great!

If you prefer, feel free to download the script and inspect it before running it.

The installation script automatically adds Rust to your system PATH after your next login. If you want to start using Rust right away instead of restarting your terminal, run the following command in your shell to add Rust to your system PATH manually:

$ source $HOME/.cargo/env

Alternatively, you can add the following line to your ~/.bash_profile:

$ export PATH="$HOME/.cargo/bin:$PATH"

Additionally, youll need a linker of some kind. Its likely one is already installed, but when you try to compile a Rust program and get errors indicating that a linker could not execute, that means a linker isnt installed on your system and youll need to install one manually. C compilers usually come with the correct linker. Check your platforms documentation for how to install a C compiler. Also, some common Rust packages depend on C code and will need a C compiler. Therefore, it might be worth installing one now.

Installing rustup on Windows

On Windows, go to https://www.rust-lang.org/install.html and follow the instructions for installing Rust. At some point in the installation, youll receive a message explaining that youll also need the C++ build tools for Visual Studio 2013 or later. The easiest way to acquire the build tools is to install Build Tools for Visual Studio 2017. The tools are in the Other Tools and Frameworks section.

The rest of this book uses commands that work in both cmd.exe and PowerShell. If there are specific differences, well explain which to use.

Updating and Uninstalling

After youve installed Rust via rustup, updating to the latest version is easy. From your shell, run the following update script:

$ rustup update

To uninstall Rust and rustup, run the following uninstall script from your shell:

$ rustup self uninstall

Troubleshooting

To check whether you have Rust installed correctly, open a shell and enter this line:

$ rustc --version

You should see the version number, commit hash, and commit date for the latest stable version that has been released in the following format:

rustc x.y.z (abcabcabc yyyy-mm-dd)

If you see this information, you have installed Rust successfully! If you dont see this information and youre on Windows, check that Rust is in your %PATH% system variable. If thats all correct and Rust still isnt working, there are a number of places you can get help. The easiest is the #rust IRC channel on irc.mozilla.org, which you can access through Mibbit. At that address you can chat with other Rustaceans (a silly nickname we call ourselves) who can help you out. Other great resources include the Users forum and Stack Overflow.

Local Documentation

The installer also includes a copy of the documentation locally, so you can read it offline. Run rustup doc to open the local documentation in your browser.

Any time a type or function is provided by the standard library and youre not sure what it does or how to use it, use the application programming interface (API) documentation to find out!