In this lesson, you will learn how to use Cargo to automatically generate the standard structure of a Rust project so your code is organized exactly how the Rust compiler expects it.
Why use Cargo?
While you could technically write Rust code in a single file and compile it manually, professional Rust projects rely on Cargo, our build system and package manager. Cargo creates a consistent "scaffolding" (or project skeleton) that ensures your code, dependencies, and configuration are always in the right place.
Creating your project
To start a new project, open your terminal and type:
cargo new my_project
This command generates a folder named my_project containing two primary things:
1. Cargo.toml
This is your project’s manifest file. It contains metadata about your program (like its name and version) and, later on, will list the external libraries your project needs to function.
2. The src directory
Inside your project folder, Cargo creates a src folder. This is where all your Rust source code must live. By convention, Cargo looks for a file named main.rs inside this folder as the entry point for your application. If you open src/main.rs, you will see a simple "Hello, world!" program already waiting for you.
Why this structure matters
By following this standard, the Rust ecosystem remains predictable. When you move to a more complex project, you will always know that your configuration is in the root and your logic is in src/. Because Cargo handles the file structure, you don't have to worry about complex build scripts or manual path configuration; Cargo knows exactly how to find your files and compile them into a runnable program.
Once you have this folder structure, you can simply run cargo run inside your project directory, and Cargo will handle the compilation and execution for you in one step.
Key Takeaway: Cargo automates project creation by setting up a Cargo.toml manifest and a src directory, ensuring your Rust project follows the idiomatic structure that the toolchain expects.