Cargo manages the build system and package management of Rust language, so it is a package manager and builds a system for Rust language. Most of the rust developers and users use Cargo in their projects as it makes tasks easy like build, downloading the project dependencies and their builds.
Cargo comes in handy when you deal with big projects where you have a lot of dependency modules and you need management of build and versions.
So now we know what is Cargo , now let’s verify its installation on our machine. So go to terminal and type the command:
> cargo –version
You should see the version number. If you don’t then you will need to install cargo separately.
Hello Cargo World!
Now we are ready to create our first program in Cargo. Open your VS code and navigate to the folder in the terminal where you want to create your project. Use the below command to create a new project:
cargo new helloCargoWorld - - bin
So now your project is created as executable application which we specified with “- – bin” option. You can verify the content files which got created with this project.
You will be able to see files like:
Src\main.rs – This is file where we will write our rust code.
cargo.toml – This is a file which have the project package dependencies details.
.gitignore – This is helpful to manage the project in git.
To build this cargo project use the below command in the terminal:
> cargo build
Once your build succeed then you should be able to see the exe file with the name “helloCargoWorld.exe”.
Now you can run this exe file on terminal by just typing the name of exe and you should be able to see code executes which we wrote in main.rs file.
Also if you want to build and run using single command then you can use below command that builds and run your program. So just type on your terminal:
Also if you just want to check your code for any build errors then you can use below command:
Remember this command will only check or compile your code for any build issues, but it will not create any executables from that code.