Lesson 1 of 26
Your first Rust project
Before any syntax, start a project the way you would in Python. Rust comes with Cargo, one tool that
creates projects, downloads dependencies, builds, runs and tests. It plays the role of uv, plus a few more.
If you don't have Rust yet, install it with rustup.
You can also do every exercise in this course in the browser.
One word you will see everywhere: a crate is one Rust package, the unit Cargo builds. A new project is one crate.
Create a project and run it
Three commands on each side. cargo run prints Hello, world! and
uv run hello prints Hello from hello!. There is no virtual environment to create or activate:
Cargo keeps everything it builds inside the project.
uv init hello
cd hello
uv run hellocargo new hello
cd hello
cargo runThe files you get
A new crate is tiny: one config file and one source file. The Python side has a few more files, but the idea
is the same. After the first build a target/ folder appears. It holds everything Cargo builds, a bit like
.venv/. It is already in .gitignore, and you can delete it any time.
hello/
โโโ .gitignore
โโโ .python-version
โโโ README.md
โโโ pyproject.toml
โโโ src/
โโโ hello/
โโโ __init__.pyhello/
โโโ .gitignore
โโโ Cargo.toml
โโโ src/
โโโ main.rsCargo.toml next to pyproject.toml
This is the whole Cargo.toml that cargo new writes, next to the important part of
pyproject.toml. There is no [project.scripts] in Rust: src/main.rs is the
program, and the program gets the crate's name.
[project]
name = "hello"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
[project.scripts]
hello = "hello:main"
[build-system]
requires = ["uv_build>=0.9,<1"]
build-backend = "uv_build"[package]
name = "hello"
version = "0.1.0"
edition = "2024"
[dependencies]The program itself
cargo new writes this src/main.rs. On the Python side, here is the same idea as
a script you can run directly. Rust needs no if __name__ == "__main__": the program always starts at
fn main.
def main() -> None:
print("Hello, world!")
if __name__ == "__main__":
main()fn main() {
println!("Hello, world!");
}Hello, world!Check, run, build
Three commands you will use all the time. cargo check only looks for errors, so it is the
fastest. cargo build makes the program file without running it. Python has no build step.
uv build makes a package to share, which is a different job.
uvx mypy src
uv run hello
.venv/bin/hellocargo check
cargo run
cargo build
./target/debug/helloWhere the Python analogy breaks
- Rust has a build step.
cargo runfirst compiles your code into a program intarget/debug/, then runs that program. Python reads your.pyfiles every time it runs them. - There is no virtual environment to create or activate.
target/holds build output, not installed packages, and you can delete it any time: the nextcargo buildmakes it again. - The finished program doesn't need Rust. You can copy
target/release/helloto another machine with the same OS and CPU type and run it there. A Python script always needs a Python interpreter. - Only
mainruns. Python runs every line at the top of a file, which is why it needsif __name__ == "__main__":. In Rust only definitions (fn,struct,use,const) sit at the top of a file. Code that does something lives inside a function.
Cheat card
| Python | Rust |
|---|---|
uv init hello | cargo new hello |
uv run hello | cargo run |
mypy src | cargo check |
# no build step | cargo build // program in target/debug/ |
pyproject.toml | Cargo.toml |
[project] | [package] |
dependencies = [] | [dependencies] |
def main(): ... | fn main() { } |
if __name__ == "__main__": main() | // not needed: Rust starts at main() |
.venv/ | target/ |