Skip to main content
Lesson 2 of 12Beginner⏱ 10 mins

Setting Up Your Environment

Install Rust, create a project, and understand Cargo.

8% of path

Learning Objectives

  • Install Rust
  • Create a new project
  • Understand Cargo.toml

##What you need before you write any code

Actix Web is a Rust library, so everything starts with the Rust toolchain. The toolchain is installed and managed by a small tool called rustup, and it gives you three things you will use every day:

  • rustc, the compiler that turns your code into a fast native program.
  • cargo, the build tool and package manager. It creates projects, downloads dependencies, compiles, runs tests and more.
  • rustfmt and clippy, a formatter and a linter that keep your code tidy and catch common mistakes.

#Install Rust

On Linux and macOS, open a terminal and run the official installer from rustup.rsopen_in_new. On Windows, download and run rustup-init.exe from the same site. When the installer asks, choose the default installation.

After it finishes, open a new terminal (so your PATH is refreshed) and check the versions:

terminalbash
rustc --version
cargo --version

If you see version numbers, you are ready. Actix Web 4 needs a reasonably recent stable compiler, so run rustup update if your Rust is old.

info
On Windows you also need the C++ build tools from Visual Studio so Rust can link programs. The installer tells you if they are missing.

#Create your first project

Cargo creates a ready-to-run project with one command:

terminalbash
cargo new hello-actix
cd hello-actix

This gives you a tiny layout:

code_blockstext
hello-actix/
  Cargo.toml     <- project name, version and dependencies
  src/
    main.rs      <- the program entry point

#Add Actix Web as a dependency

Dependencies live in Cargo.toml. The easiest way to add one is with cargo add:

terminalbash
cargo add actix-web

Open Cargo.toml and you will see a line like this under [dependencies]:

code_blockstoml
[package]
name = "hello-actix"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4"

The "4" means "any compatible 4.x release". Cargo downloads the crate and everything it needs the first time you build, which can take a minute. Later builds are much faster because Cargo caches the work.

#Check that everything compiles

Run the default program that Cargo generated:

terminalbash
cargo run

You should see Hello, world!. That proves your compiler, linker and Cargo are all working. In the next lessons you will replace that program with a real web server.

#Handy tools to install now

  • rust-analyzer: the editor plugin for VS Code and other editors. It gives you autocomplete, inline errors and type hints, which makes Rust far easier to learn.
  • cargo-watch (optional): cargo install cargo-watch, then cargo watch -x run restarts your server whenever you save a file.

Take a moment to run cargo check. It compiles without producing a binary, so it is the fastest way to see whether your code is valid while you work.

"What If?" Experiment

Learning by doing. Modify the code to see what happens.

Goal

See how Cargo records a dependency.

Starting Code
[dependencies] actix-web = "4"
Suggested Modification

Add serde with cargo add serde --features derive, then open Cargo.toml again.

Expected Result

A new serde line with features = ["derive"] appears under [dependencies].

Explanation: cargo add edits Cargo.toml for you. Features switch optional parts of a crate on, and serde needs the derive feature to generate Serialize and Deserialize implementations.

Common Mistakes

Anticipate and avoid these frequent pitfalls when working with this concept.

!

Running cargo commands outside the project folder

The terminal is still in your home directory, so Cargo cannot find a Cargo.toml.

Incorrect Pattern
$ cargo run error: could not find `Cargo.toml` in `/home/me` or any parent directory
Correct Pattern
$ cd hello-actix $ cargo run

Explanation: Cargo always looks for Cargo.toml in the current folder (and its parents). Change into the project directory first.

!

Using an old Rust version

The system package manager installed an outdated compiler, and Actix Web 4 needs a recent one.

Incorrect Pattern
error: package `actix-web` requires rustc 1.72 or newer
Correct Pattern
$ rustup update stable

Explanation: Install Rust through rustup rather than the OS package manager and update it regularly, so you always have a modern stable compiler.

Practical Exercise

Create and run your own project

Task

Create a new Cargo project called my-api, add Actix Web, and confirm the project builds.

Requirements

  • Create a project named my-api with cargo new
  • Add actix-web with cargo add
  • Run cargo check and see it finish without errors

Need a hint?

Try solving the exercise in your local environment first. Once you have a solution, or if you get completely stuck, reveal the answer.

Real-World Application

Every Rust backend team uses exactly this setup: rustup on developer machines and in CI, Cargo.toml as the single source of truth for dependencies, and cargo check or clippy on every commit to keep the codebase healthy.

Key Takeaways

  • Install Rust with rustup; it gives you rustc, cargo, rustfmt and clippy.
  • cargo new creates a project, cargo add actix-web adds the framework, cargo run builds and runs it.
  • Dependencies are declared in Cargo.toml, and "4" means any compatible 4.x release.
  • Use cargo check for fast feedback and install rust-analyzer in your editor.

Finished reading?

Mark this lesson as done to track your progress and earn XP.