Hello, Rust
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 compilercargo— the thing you will actually userustup— 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.
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
Every piece of "hello world" in one picture
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
cargo new hellomade a directory with aCargo.toml(the project manifest) and asrc/main.rs(the code).cargo runasked the compiler to turnsrc/main.rsinto a native binary attarget/debug/hello, then ran it.- 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.
- Change
"Hello, world!"to your name. Run it. - Add a second
println!on the next line. Run it. - 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.
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.