Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hello, Rust

If you only remember one thing: installing Rust is one command, and from that point on cargo is the only tool you will touch.

Install in one command

Open a terminal and run:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Accept the default when it asks. When it finishes, close the terminal and open a new one. You now have:

  • rustc — the compiler
  • cargo — the thing you will actually use
  • rustup — the updater you will forget exists

If you are uncomfortable curl-pipe-shelling a script, the same installer is available as a native package on every major OS. The official instructions at rust-lang.org/tools/install list every option.

Plain English

Rust's package manager is called Cargo. Cargo is npm, pip, pipenv, virtualenv, make, and the test runner — all in one binary, the same on every OS. You will use four Cargo commands in this whole book. You will learn them in the next paragraph.

The four Cargo commands you actually use

cargo new hello      # make a new project
cargo run            # compile and run it
cargo test           # run the tests
cargo build --release  # make the fast binary for production

That is it. There are many more Cargo commands, but these four will carry you through the first three months.

Hello, world

Run this:

cargo new hello
cd hello
cargo run

Cargo made a tiny project for you and ran it. You will see something like:

   Compiling hello v0.1.0 (/tmp/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.81s
     Running `target/debug/hello`
Hello, world!

Open src/main.rs in your editor. It looks like this:

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

▶ Run this in the Rust Playground

Anatomy

Every piece of "hello world" in one picture

fn main () { println ! ( "Hello, world!" ); } "fn" declares a function "main" is where every program starts "!" means this is a macro, not a function semicolons end statements

Read the picture. That is the whole program. The only piece that will surprise a Python or JavaScript reader is the ! after println. It marks a macro — a compile-time code generator. Rust has macros so that tiny features like string formatting do not need to be built into the language itself. For now, treat println! as just “the thing that prints”.

What just happened

The flow
  1. cargo new hello made a directory with a Cargo.toml (the project manifest) and a src/main.rs (the code).
  2. cargo run asked the compiler to turn src/main.rs into a native binary at target/debug/hello, then ran it.
  3. Your OS printed Hello, world! to the terminal. No virtual machine, no interpreter — just a binary, the same kind a C program would have produced.

What actually happened when you typed those commands? Step through the journey from source file to text on your screen.

Interactive simulation (requires JavaScript): cargo run compiles your source with rustc into a self-contained native binary, the OS loads it and calls main, and println! writes the literal (stored in the binary itself) to standard output.

## Try this
Two-minute exercises
  1. Change "Hello, world!" to your name. Run it.
  2. Add a second println! on the next line. Run it.
  3. Delete one of the semicolons on purpose and run it. Read the error Rust gives you. Notice how it points exactly at the character that is wrong. That is what Rust errors look like. You will read many of them this week.
Plain English

You just compiled a native binary with one command. The same binary will run on this machine without Rust installed. That is what "systems language" means: you are producing the real thing, not a script that needs an interpreter on the other end.

Next up: the let keyword, and Rust’s quiet opinion that data should not change unless you ask it to.

Values, names, and the “let” word →