Skip to main content
St Louis

Back to all posts

How to Create A New Rust Project?

Published on
6 min read
How to Create A New Rust Project? image

Best Tools for Rust Projects to Buy in October 2025

1 Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language

Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language

BUY & SAVE
$29.50
Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language
2 Write Powerful Rust Macros

Write Powerful Rust Macros

BUY & SAVE
$46.72 $59.99
Save 22%
Write Powerful Rust Macros
3 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$29.99
The Rust Programming Language, 2nd Edition
4 Rust Programming Language - Developer Tools and Libraries T-Shirt

Rust Programming Language - Developer Tools and Libraries T-Shirt

  • BUILD EFFICIENT, RELIABLE SOFTWARE WITHOUT RUNTIME OVERHEAD.
  • SEAMLESS INTEGRATION WITH LANGUAGES FOR DIVERSE APPLICATIONS.
  • FRIENDLY COMPILER AND SMART TOOLS FOR ENHANCED CODING EFFICIENCY.
BUY & SAVE
$19.99
Rust Programming Language - Developer Tools and Libraries T-Shirt
5 Rust Programming Language - Developer Tool for Collaborating T-Shirt

Rust Programming Language - Developer Tool for Collaborating T-Shirt

  • MEMORY-EFFICIENT RUST: NO RUNTIME, NO GARBAGE COLLECTOR NEEDED!
  • INTEGRATES EASILY, IDEAL FOR PERFORMANCE-CRITICAL SERVICES.
  • FRIENDLY COMPILER & TOOLS HELP YOU BUILD RELIABLE SOFTWARE FAST!
BUY & SAVE
$19.99
Rust Programming Language - Developer Tool for Collaborating T-Shirt
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
+
ONE MORE?

To create a new Rust project, follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command: cargo new ProjectName. Replace ProjectName with the desired name for your project.
  4. The cargo new command generates the basic structure for your Rust project, including a src directory with a main.rs file.
  5. Navigate into the project folder using: cd ProjectName.
  6. Open the src/main.rs file in a text editor of your choice.
  7. Write your Rust code within the main.rs file to define the behavior of your project.
  8. Once you've written your code, save the file.
  9. To build and run your project, use the cargo run command in the terminal.
  10. The terminal will display the output of your Rust program.

These steps will enable you to create a new Rust project, write your code, and run it successfully.

What is the cargo test command used for in Rust projects?

The cargo test command in Rust is used to run the tests present in a Rust project. It compiles and executes the test code written within the tests directory of the project. These tests are written using the Rust's built-in testing framework called test.

Running cargo test command compiles all the source code present in the project along with the test code, and runs the tests in parallel. It displays the test results, indicating which tests passed and which failed, along with any test output or error messages.

The cargo test command also provides several options to specify which tests to run, such as filtering by test name or module path, running tests in parallel or sequentially, etc. Additionally, it allows customization of test execution by manipulating the Cargo.toml file or by passing command-line arguments.

The cargo test command is an essential tool for ensuring the correctness of code by automatically running tests during development, enabling developers to catch and fix any issues or regressions.

What is the default directory structure of a new Rust project?

The default directory structure of a new Rust project includes the following folders and files:

  1. src/ - This directory contains the source code files of the project. The main Rust code file (entry point) is usually located here with the name main.rs. Other modules and library files can be organized in this directory as well.
  2. Cargo.toml - This is the manifest file for the Rust project, which specifies the project's metadata and dependencies. It is written in TOML format and includes information like the project name, version, authors, and dependencies.
  3. Cargo.lock - This file is automatically generated by Cargo, the Rust package manager, and it keeps track of the exact versions of the dependencies used in the project. It is used to ensure reproducible builds by locking the dependency versions.
  4. target/ - This directory is created by Cargo and contains the compiled output of the project. It includes the executable binary or library files along with any intermediate build artifacts.
  5. tests/ - This directory is used to store integration and unit tests for the project. Rust's built-in testing framework can be used to write and run tests in this directory.
  6. examples/ - This directory is used to store example code or applications that showcase how to use the project as a library or executable.

It's important to note that this is the default structure, but Rust allows flexibility in organizing the project according to your needs.

How to create documentation for a Rust project using Cargo?

To create documentation for a Rust project using Cargo, you can follow these steps:

  1. Make sure your Rust project has the necessary documentation comments in your code. Documentation comments are written using /// or //! and are used to describe the purpose and usage of your functions, structs, and modules.
  2. Open a terminal or command prompt and navigate to the root directory of your Rust project.
  3. Run the following command:

cargo doc

This will tell Cargo to generate the documentation for your project.

  1. After the command completes, Cargo will create a target/doc directory in your project's root directory. This directory contains the HTML documentation for your project.
  2. To view the documentation locally, you can open the index.html file in your browser from the target/doc directory. Alternatively, you can use the following command to start a local server and open the documentation in your browser automatically:

cargo doc --open

  1. If you want to publish your project's documentation online, you can use a documentation hosting service like docs.rs or GitHub Pages. For docs.rs, you can simply publish your documentation by pushing your Rust project to a GitHub repository and enabling the "docs.rs" service for that repository. For GitHub Pages, you can configure it to publish the contents of the target/doc directory.

By following these steps, you can generate and publish documentation for your Rust project using Cargo.

What is the purpose of the registry in a Rust project?

The purpose of the registry in a Rust project is to manage the dependencies of the project. It acts as a centralized database or repository that stores information about available packages or libraries (crates) that can be used in the project. The registry contains metadata, such as version numbers, dependencies, and other relevant information for each crate.

When a Rust project needs to use external dependencies, the project's Cargo.toml file specifies these dependencies, and the Cargo package manager uses the registry to resolve and fetch the required crates. The registry ensures that the correct versions of the dependencies are downloaded and installed, and handles any required dependency resolution or conflict resolution.

By using a registry, Rust projects can easily manage and update their dependencies, ensuring that the correct and compatible versions of the dependencies are used. The registry also simplifies the process of discovering and using existing crates, enabling code reuse and accelerating development.

What is the main function in a Rust project and how to create it?

The main function is the entry point for execution in a Rust project. It is the first function that is called when the program runs.

To create a main function in Rust, you need to define it with the signature "fn main()". Here's an example of a simple main function:

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

In this example, the main function uses the println! macro to print the message "Hello, world!" to the console. When you run this Rust program, the main function will be executed first, and the message will be displayed.