Creating the Rust Project

We will begin by using the cargo tool to create our project directory for us. Of the arguments that you can use, the most important ones are new, build, install and doc. What each argument does should be self-explanatory at this point. By default, the new argument will create a library project, but since we are going to create a binary executable, we need to further pass the --bin flag along with the name of the project.

cargo new --bin "phoronix-reader"

Cargo Project Directory

  • Project/
    • Cargo.toml
    • src/
      • main.rs

You will notice that a new directory has been created. Inside this directory is a Cargo.toml file as well as a src directory. We will start by adding the crates that we want Cargo to build to the Cargo.toml file. The crates that we are going to use are hyper for making a HTTP request to download the HTML of the front page of Phoronix and select for conveniently obtaining just the information we want from that HTML page. Add the following lines at the bottom of Cargo.toml:

Cargo.toml

[dependencies]
hyper = "*"
select = "*"

Adding Crates To Your Source Code

Now we can enter the src directory where we will find the main.rs source code file. The main.rs file is required when working with binary projects. It will signify to Cargo to begin compiling a binary starting from main.rs as the source. When working on a library, you will instead start writing code from the lib.rs file. Cargo will automatically detect whether to compile a binary or a library based on the existence of either file.

Inside main.rs, enter the following to import these crates into the project:

extern crate hyper;
extern crate select;