Rust for Pythonistas

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.

Terminal ยท Python
uv init hello
cd hello
uv run hello
Terminal ยท Rust
cargo new hello
cd hello
cargo run

The 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.

uv init hello
hello/
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ .python-version
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ pyproject.toml
โ””โ”€โ”€ src/
    โ””โ”€โ”€ hello/
        โ””โ”€โ”€ __init__.py
cargo new hello
hello/
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ Cargo.toml
โ””โ”€โ”€ src/
    โ””โ”€โ”€ main.rs

Cargo.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.

pyproject.toml (shortened)
[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"
Cargo.toml
[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.

Python
def main() -> None:
    print("Hello, world!")


if __name__ == "__main__":
    main()
Rust
fn main() {
    println!("Hello, world!");
}
Output
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.

Terminal ยท Python
uvx mypy src
uv run hello
.venv/bin/hello
Terminal ยท Rust
cargo check
cargo run
cargo build
./target/debug/hello

Where the Python analogy breaks

Cheat card

PythonRust
uv init hellocargo new hello
uv run hellocargo run
mypy srccargo check
# no build stepcargo build // program in target/debug/
pyproject.tomlCargo.toml
[project][package]
dependencies = [][dependencies]
def main(): ...fn main() { }
if __name__ == "__main__": main()// not needed: Rust starts at main()
.venv/target/

A tiny app: a function and a module โ†’