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

Understanding Cargo and Project Setup

In the previous chapter, we looked at the structure of the Weather Application and the widgets used to build the user interface.

Now we will learn about Cargo, the tool that manages Rust projects.

Cargo helps us build, run, and organize our application. It also manages external libraries that our project depends on.

Understanding Cargo will help you see how Rust code, GTK libraries, and external packages work together to create a complete application.

What Is Cargo?

Cargo is the official build tool and package manager for Rust.

It performs several important tasks for developers.

Cargo can:

  • Create new Rust projects
  • Download external libraries (called crates)
  • Compile the program
  • Run the application
  • Manage project versions and dependencies

Instead of manually compiling many files, Cargo handles everything automatically.

When we run a command such as:

cargo run

Cargo first compiles the project and then runs the application.

Because of this, Cargo becomes the central tool used in almost every Rust project.

The Cargo.toml File

Every Rust project contains a configuration file called Cargo.toml.

This file tells Cargo important information about the project, such as:

  • The project name
  • The version of the application
  • The Rust edition used
  • The external libraries required by the project

Open the Cargo.toml file in the Weather App project. You will see something similar to this:

[package]
name = "weatherapp"
version = "0.1.0"
edition = "2021"
build = "build.rs"

[dependencies]
gtk4 = "0.9"
libadwaita = { version = "0.7", features = ["v1_4"] }

reqwest = { version = "0.11", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
urlencoding = "2.1"

[build-dependencies]
glib-build-tools = "0.18"

This file defines how the project is built and which libraries are used.

Project Information

The [package] section contains basic information about the project.

[package]
name = "weatherapp"
version = "0.1.0"
edition = "2021"

The name defines the name of the application. Cargo will create an executable file with this name.

The version shows the current version of the project. Version numbers become important when distributing applications.

The edition defines the Rust language version used by the project. Using the 2021 edition enables modern Rust features.

Project Dependencies

The [dependencies] section lists external libraries used by the application.

Rust libraries are called crates.

When you build the project, Cargo automatically downloads these crates from the Rust package registry.

For example, the Weather App uses several important crates.

GTK4

gtk4 = "0.9"

This crate provides Rust bindings for the GTK4 graphical user interface toolkit.

It allows us to create windows, buttons, labels, and other interface elements.

Libadwaita

libadwaita = { version = "0.7", features = ["v1_4"] }

Libadwaita provides modern GNOME interface components.

Widgets such as:

  • HeaderBar
  • NavigationSplitView
  • StatusPage
  • ActionRow

come from this library.

These widgets help create applications that follow GNOME design guidelines.

reqwest

reqwest = { version = "0.11", features = ["blocking", "json"] }

The reqwest crate is used to send HTTP requests.

In this Weather App, it is used to fetch weather data from the Open-Meteo API.

The enabled features allow the program to:

  • Send requests easily (blocking)
  • Automatically handle JSON responses (json)

serde

serde = { version = "1.0", features = ["derive"] }

The serde crate is used to convert JSON data into Rust data structures.

Weather APIs usually return data in JSON format. Serde allows the application to easily convert this data into Rust variables.

urlencoding

urlencoding = "2.1"

This crate ensures that city names are correctly formatted when used in web requests.

For example:

New York → New%20York

This encoding prevents errors when sending requests to the weather API.

Build Dependencies

Some tools are only needed during compilation.

These are defined in the [build-dependencies] section.

[build-dependencies]
glib-build-tools = "0.18"

In the Weather App, this tool is used to compile GTK resource files such as .ui interface files.

This process is triggered by a special script called build.rs.

How Cargo Builds the Application

When you run the following command:

cargo build

Cargo performs several steps automatically.

First, it checks the dependencies listed in Cargo.toml. Then it downloads the required crates from the Rust package registry.

After that, it compiles all crates and links them together with the project code.

Finally, Cargo produces the application executable.

The compiled files are placed in the target directory.

Running the Application

To build and run the Weather App, you can use the following command:

cargo run

Cargo will compile the project and start the application.This command is very convenient during development because it performs both steps automatically.

open the real file in the project

You can also open the file directly from the project folder:

weather-app/Cargo.toml