Sling Academy
Home/Rust/Creating a Minimal Rust Project with Cargo and Default Package Structure

Creating a Minimal Rust Project with Cargo and Default Package Structure

Last updated: January 04, 2025

Rust is a modern and powerful programming language that aims for safety and performance. Cargo, Rust's package manager and build system, simplifies the process of setting up, building, and managing Rust projects. In this guide, we'll walk through the process of creating a minimal Rust project using Cargo and explore the default package structure it provides.

Setting Up Your Environment

Before diving into project creation, ensure you have Rust and Cargo installed. Check your current installation by running the following in your terminal:

rustc --version
cargo --version

If these commands return version numbers, you’re ready to go. Otherwise, refer to the official Rust website for the installation instructions.

Creating a New Rust Project

To create a new Rust project, use Cargo's new command. By default, Cargo sets up a new project with a binary crate. Open your terminal and navigate to your desired projects directory, then execute the following command:

cargo new hello_world

This command creates a new directory named hello_world with a basic package structure:

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

Exploring the Default Package Structure

Cargo.toml

This file is the manifest for Rust projects. It includes metadata about your project like its name, version, dependencies, and more. When you run Cargo commands, it reads this file to know what to do.

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

[dependencies]

You can define dependencies your project needs in the [dependencies] section, but for now, it’s empty. Cargo will manage all your package dependencies here.

The src Directory

The src directory is where you write your code. It contains a default file named main.rs, which is the entry point for the Rust application:

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

This simple program demonstrates Rust's syntax for functions and macros, like println! which outputs to the console.

Building and Running Your Project

After setting up your minimal project, you can compile and run it using Cargo:

cd hello_world
cargo build

This command compiles your project and the resulting binary locates in the target/debug/ directory. For a succinct experience, use cargo run to compile and immediately execute it:

cargo run

You should see the output:

Hello, world!

Congratulations! You've just created and run a minimal Rust project.

Exploring More Commands

Cargo provides additional commands like cargo test for running tests, and cargo doc for generating documentation. As your project grows, these commands become instrumental in managing code quality and documentation.

Conclusion

Getting started with Rust and Cargo is straightforward. By following the steps in this guide, you've not only set up a minimal Rust project but also gained insights into its structure. Now you can expand upon this foundation by adding your own functionality and experimenting with libraries. Rust and Cargo provide powerful tools to grow your skills and applications, making them well worth mastering.

Next Article: Defining a Library Crate vs a Binary Crate in Rust

Previous Article: Understanding the Distinctions Among Packages, Crates, and Modules in Rust

Series: Packages, Crates, and Modules in Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior