Rust: Cargo Init vs Cargo New Command


The cargo new Command

    The cargo new command is used to create a new Rust project as a new directory.

    Example: cargo new

    % cargo new myproj1
    
         Created binary (application) `myproj1` package
    
    % cd myproj1 
    
    % ls -l
    
    total 8
    -rw-r--r--  1 c2ctechtv  staff  176 Jul 18 23:27 Cargo.toml
    drwxr-xr-x  3 c2ctechtv  staff   96 Jul 18 23:27 src

    The cargo new command generated the directory for the project as well as the Cargo.toml file as well as the source folder containing main.rs file.



The cargo init Command


    The cargo init command is used to initialize an existing directory as a Rust project. You can think of it as a git init command.

    Example: cargo init

    % mkdir myproj2
    
    % cd myproj2 
    
    % cargo init
         Created binary (application) package
    
    % ls -l
    total 8
    -rw-r--r--  1 c2ctechtv  staff  176 Jul 18 23:34 Cargo.toml
    drwxr-xr-x  3 c2ctechtv  staff   96 Jul 18 23:34 src
    Code2care@Mac % 

As you can see, I created a folder named myproj2 first then did a cd followed by cargo init this generated the directory for the project as well as the Cargo.toml file as well as the source folder containing main.rs file.

Examples Rust Cargo new and init Commands

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap