Installing Rust
You can try Rust online in the Rust Playground without installing anything on your computer.
Try Rust without installingRustup: the Rust installer and version management tool
The primary way that folks install Rust is through a tool called Rustup, which is a Rust installer and version management tool.
It looks like you’re running macOS, Linux, or another Unix-like OS. To download Rustup and install Rust, run the following in your terminal, then follow the on-screen instructions.
curl https://sh.rustup.rs -sSf | sh
It looks like you’re running Windows. To install Rust, download and run the following, and then follow the onscreen instructions.
rustup‑init.exeWindows Subsystem for Linux
If you’re a Windows Subsystem for Linux user run the following in your terminal, then follow the on-screen instructions to install Rust.
curl https://sh.rustup.rs -sSf | sh
Rust runs on Windows, Linux, macOS, FreeBSD and NetBSD. If you are on one of these platforms and are seeing this then please report an issue with the following values:
Report an Issue
To install Rust, if you are running Unix,
run the following in your terminal, then follow the on-screen instructions.
curl https://sh.rustup.rs -sSf | sh
If you are running Windows,
download and run rustup‑init.exe then follow the on-screen instructions.
curl https://sh.rustup.rs -sSf | sh
If you are running Windows,
download and run rustup‑init.exe then follow the on-screen instructions.
Learn more about installation
Cargo: the Rust build tool and package manager
When you install Rustup you’ll also get the latest stable version of the Rust build tool and package manager, also known as Cargo. Cargo does lots of things:
- build your project with
cargo build
- run your project with
cargo run
- test your project with
cargo test
- build documentation for your project with
cargo doc
- publish a library to crates.io with
cargo publish
To test that you have Rust and Cargo installed, you can run this in your terminal of choice:
cargo --version
Other tools
Rust support is available in many editors:
You can install a code formatting tool (Rustfmt) with rustup component add rustfmt
, and a linting tool (Clippy) with rustup component add clippy
.
Generating a new project
Let’s write a small application with our new Rust development environment. To start, we’ll use Cargo to make a new project for us. In your terminal of choice run:
cargo new hello-rust
This will generate a new directory called hello-rust
with the following files:
hello-rust
|- Cargo.toml
|- src
|- main.rs
Cargo.toml
is the manifest file for Rust. It’s where you keep metadata for your project, as well as dependencies.
src/main.rs
is where we’ll write our application code.
cargo new
generates a "Hello, world!" project for us! We can run this program by moving into the new directory that we made and running this in our terminal:
cargo run
You should see this in your terminal:
$ cargo run
Compiling hello-rust v0.1.0 (/Users/ag_dubs/rust/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 1.34s
Running `target/debug/hello-rust`
Hello, world!
Adding dependencies
Let’s add a dependency to our application. You can find all sorts of libraries on crates.io, the package registry for Rust. In Rust, we often refer to packages as “crates.”
In this project, we’ll use a crate called ferris-says
.
In our Cargo.toml
file we’ll add this information (that we got from the crate page):
[dependencies]
ferris-says = "0.1"
Now we can run:
cargo build
...and Cargo will install our dependency for us.
You’ll see that running this command created a new file for us, Cargo.lock
. This file is a log of the exact versions of the dependencies we are using locally.
To use this dependency, we can open main.rs
, remove everything that’s in there (it’s just another example), and add this line to it:
use ferris_says::say;
This line means that we can now use the say
function that the ferris-says
crate exports for us.
A small Rust application
Now let’s write a small application with our new dependency. In our main.rs
, add the following code:
use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let out = b"Hello fellow Rustaceans!";
let width = 24;
let mut writer = BufWriter::new(stdout.lock());
say(out, width, &mut writer).unwrap();
}
Once we save that, we can run our application by typing:
cargo run
Assuming everything went well, you should see your application print this to the screen:
----------------------------
| Hello fellow Rustaceans! |
----------------------------
\
\
_~^~^~_
\) / o o \ (/
'_ - _'
/ '-----' \
Learn more!
You’re a Rustacean now! Welcome! We’re so glad to have you. When you’re ready, hop over to our Learn page, where you can find lots of books that will help you to continue on your Rust adventure.
learn more!Who’s this crab, Ferris?
Ferris is the unofficial mascot of the Rust Community. Many Rust programmers call themselves “Rustaceans,” a play on the word “crustacean.” We refer to Ferris with the pronouns “they,” “them,” etc., rather than with gendered pronouns.
Ferris is a name playing off of the adjective, “ferrous,” meaning of or pertaining to iron. Since Rust often forms on iron, it seemed like a fun origin for our mascot’s name!
You can find more images of Ferris on http://rustacean.net/.