Purpose of This Chapter

In this foundational chapter, you will set up your development environment by installing Rust and its accompanying package manager, Cargo. You will then initialize a new Rust project, which will serve as the base for our password generator CLI application. Getting this right is crucial for a smooth development process.

Concepts Explained

Rust: A modern systems programming language known for its speed, memory safety, and parallelism. It’s an excellent choice for CLI tools due to its performance and the ability to compile to a single, self-contained binary.

Cargo: Rust’s official package manager and build system. It handles everything from creating new projects and managing dependencies to compiling code and running tests. Think of it as npm for JavaScript or pip for Python.

Project Structure: A typical Rust project initialized with Cargo follows a standard directory layout. This structure helps organize your code, making it easier to navigate and maintain, especially as the project grows.

Step-by-Step Tasks

1. Install Rust and Cargo

If you don’t have Rust and Cargo installed, open your terminal or command prompt and run the appropriate command for your operating system.

For Linux/macOS: The rustup script will download and install rustup, which then installs the latest stable version of Rust and Cargo.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Follow the on-screen instructions. When prompted, it’s generally safe to proceed with option 1 (default installation). After installation, you might need to restart your terminal or source your shell’s profile (e.g., source $HOME/.cargo/env) to ensure Cargo is in your PATH.

For Windows: Visit the official Rust website: https://rustup.rs/ and download the rustup-init.exe installer. Run the installer and follow the prompts. It will set up Rust and Cargo for you.

To verify the installation, open a new terminal or command prompt and run:

rustc --version
cargo --version

You should see output similar to this (versions may vary):

rustc 1.74.0 (79e9716c9 2023-11-13)
cargo 1.74.0 (ec169b6a5 2023-11-07)

2. Initialize Your New Rust Project

Now, let’s create our password generator project. We’ll name it rpassword-gen.

Navigate to your desired development directory and run:

cargo new rpassword-gen --bin
  • cargo new: The command to create a new Rust project.
  • rpassword-gen: The name of our project.
  • --bin: Specifies that we are creating an executable application (a binary crate) rather than a library.

This command creates a new directory named rpassword-gen with the following basic structure:

rpassword-gen/
├── Cargo.toml
└── src/
    └── main.rs
  • Cargo.toml: The manifest file for your project. It contains metadata about your project and its dependencies.
  • src/main.rs: The main source file for your executable. This is where your application’s logic will reside.

3. Explore the Initial Project

Open the Cargo.toml file. It will look something like this:

[package]
name = "rpassword-gen"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
  • name: The name of your package.
  • version: The current version of your package.
  • edition: The Rust edition to use (usually the latest stable).
  • [dependencies]: This section is where we will list all external libraries our project relies on.

Now, look at src/main.rs:

fn main() {
    println!("Hello, world!");
}

This is the classic “Hello, World!” program. fn main() is the entry point of every executable Rust program. println! is a macro that prints text to the console.

4. Run Your Project

To ensure everything is working correctly, navigate into your new project directory and run it:

cd rpassword-gen
cargo run

You should see the output:

   Compiling rpassword-gen v0.1.0 (/path/to/rpassword-gen)
    Finished dev [unoptimized + debuginfo] target(s) in X.XXs
     Running `target/debug/rpassword-gen`
Hello, world!
  • cargo run: Compiles your project (if changes have been made) and then executes the resulting binary. The first time you run it, Cargo will download and compile Rust’s standard library and your project, which can take a moment. Subsequent runs will be faster.

Summary/Key Takeaways

In this chapter, you successfully:

  • Installed Rust and Cargo, the essential tools for Rust development.
  • Initialized a new binary Rust project named rpassword-gen.
  • Understood the basic structure of a Rust project (Cargo.toml and src/main.rs).
  • Ran your first Rust program, confirming your setup is correct.

With your environment ready, we can now move on to defining the command-line interface for our password generator.