How to use Cargo in Rust Programming

This tutorial explains how to use Cargo tool in Rust. Cargo is Rust's build system and package manager. Cargo comes installed with Rust installation.

For following below instructions rust and VsCodeinstallation is required, if you don't have rust installed, follow link install rust on windows.

  • Check Cargo installation version
  • 
    
    E:\rust-tutorials> cargo --version
    
    #### Output ####
    cargo 1.48.0 (65cbdd2dc 2020-10-14)
    
    
    
  • Create a new project with Cargo
  • 
    
    E:\rust-tutorials\first-project>cargo new test_cargo
    
    #### Output ####
    Created binary (application) `test_cargo` package
    
    
  • Navigate to new project and open it with VsCode
  • 
    
    E:\rust-tutorials\first-project>cd test_cargo
    
    E:\rust-tutorials\first-project\test_cargo>code .
    
    
  • You should have following structure inside "test_cargo" directory
  • 
    
      E:.
      │   .gitignore
      │   Cargo.toml
      │
      └───src
              main.rs
    
    
  • Build the Cargo project with below command
  • 
    
    E:\rust-tutorials\first-project\test_cargo>cargo build
    
    #### Output ####
    Compiling test_cargo v0.1.0 (E:\rust-tutorials\first-project\test_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 1.07s
    
    
  • Run the Cargo project
  • 
    E:\rust-tutorials\first-project\test_cargo>.\target\debug\test_cargo.exe
    
    #### Output ####
    Hello, world!