<tutorialjinni.com/>

What is Cargo Rust?

Posted Under: Programming, Rust, Rust Tutorial For Beginners, Tutorials on May 19, 2022
What is Cargo Rust?
Cargo is package manager for Rust's and its build system. Cargo is used to search for, install, and manage packages that you want to use and build them. Cargo is also the test runner, and the documentation generator. It's almost equivalent to npm, pip, bundler and make. Lets do something real, first Install Rust if you have not already, then open terminal and type.
cargo new hello_rust
Cargo will create a project named hello_rust. Let us take a look what cargo created;
D:\RUSTY\hello_rust
|   .gitignore
|   Cargo.toml
\---src
        main.rs
a hello_rust directory with a config file named cargo.toml and a source sub directory with a main.rs file. Config files in Rust use the .toml format which stands for Tom's obvious minimal language. Rust source files use the .rs extension as you see on main.rs here. Now open this project in VSCode, Setup VSCode for Rust. Open Cargo.toml, it should be similar
[package]
name = "hello_rust" Actual name of the project
version = "0.1.0" Project Version
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

Cargo.toml is the config file for your Rust project. It has the authoritative source of information about your project. name is the actual name of your project. It does not matter what is the name of the directory what the name of your Git repository is. This setting determines the name of your project. Rust uses semantic versioning, which means a version number is always three numbers separated by dots and each number has a specific meaning, for more info visit semver.org. edition should be 2018, for now, if cargo did not add this line for you automatically then you are using an older version of Rust and you need to update it now by running "rustup update".

Now open main.rs in the src directory. It already has a "Hello, world!" program for us.
fn main() {
    println!("Hello, world!");
}
Back in the terminal issue command
D:\Rusty\hello_rust>cargo run
   Compiling hello_rust v0.1.0 (D:\Rusty\hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 2.20s
     Running `target\debug\hello_rust.exe`
Hello, world! 
"cargo run" is the command to build and run your project in one step. If you run your project now you'll see "Hello, World!" right after cargo finishes building. If you run it again you'll see that cargo can tell that nothing changed in the source code so it doesn't recompile the project. Notice what cargo is running.

target\debug\hello_rust.exe
There is a target directory where cargo outputs all of its build artifacts. We can also run the binary directly if we want. Did you notice that it is in a debug sub directory? Cargo compiles your project with debug symbols by default. Now compile it again with --release switch.
D:\Rusty\hello_rust>cargo run --release
   Compiling hello_rust v0.1.0 (D:\Rusty\hello_rust)
    Finished release [optimized] target(s) in 1.89s
     Running `target\release\hello_rust.exe`
Hello, world!
You will notice a similar result, only this time it will be stored in the released sub directory. Most code will run significantly faster in release mode, but it takes a little longer to compile.


imgae