cargo Verified current stable Not installed? Package Management

Cargo / Create Init New Rust Project

Create Init New Rust Project

Initialize a new Rust project in a specified directory.

$
Terminal
cargo init --<bin|lib> <path/to/directory>

When To Use

When starting a new project requiring either a binary or library structure.

Pro Tip

Use `cargo init --name <project_name>` to name the project immediately during initialization.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
cargo init --<bin|lib> <path/to/directory>

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
Creating new Rust project at 'path/to/directory'  
   Project name: my_project  
   Type: Binary / Library  

Project initialized successfully.

Anatomy of Output

Understanding the result

Created binary (application) `my_project` Project Type

Indicates the creation of a binary project.

Created README.md File Created

The accompanying README file generated for the project.

Your project is ready! Completion Message

Confirms successful project initialization.

Power User Variants

Optimized versions

cargo init --bin --name my_project

Initialize a new binary project with a specified name.

cargo init --lib

Initialize a new library project.

Troubleshooting

Common pitfalls

error: no such directory

Solution: Verify the specified path exists or create it before running the command.

error: invalid input

Solution: Check the syntax of the command; ensure proper flags and options are provided.

error: permissions denied

Solution: Ensure write permissions are available in the specified installation directory.

Command Breakdown

What each part is doing

cargo
Base Command
The executable that performs this operation. Here it runs Cargo before the shell applies any redirect operators.
<bin|lib>
bin|lib
The value supplied for bin|lib.
<path/to/directory>
path to directory
The directory path supplied to this command.
--<bin|lib>
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Run the command: cargo init --bin path/to/directory or cargo init --lib path/to/directory

  2. Step 2

    Navigate to the specified directory: cd path/to/directory

  3. Step 3

    Check if the project files are created: ls -la

Alternative Approaches

Comparable commands in other tools

Alternative package management tools for the same job.