56. Cargo in Rust.
This is a rusty christmas and today we are learning about Cargo in rust!
- Cargo is Rust’s build system and package manager. Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. The libraries are also called as dependencies. Cargo, in terms of functionality is somewhat similar to npm.
cargo new hello_cargo --bin
creates an executable application called hello_cargo. Executables are binary executable files often called just binaries. In the hello_cargo directory, we can see that Cargo has generated two files and one directory for us: a Cargo.toml and a src directory with a main.rs file inside. It has also initialized a new git repository in the hello_cargo directory for us, along with a .gitignore file- Cargo.toml: TOML is the cargo config file format.
- In Rust, packages of code are referred to as crates.
- Cargo expects your source files to live inside the src directory. The top-level project directory is just for README files, license information, configuration files, and anything else not related to your code.
- From your hello_cargo directory, build your project by entering the following command:
cargo build
. This command creates an executable file in target/debug/hello_cargo (or target\debug\hello_cargo.exe on Windows) rather than in your current directory. Because the default build is a debug build, Cargo puts the binary in a directory named debug. - Command
cargo run
does both builds and execute the executable in one command. - Command
cargo check
quickly checks your code to make sure it compiles but doesn’t produce an executable. It's a quick way to see if your code is error free and can be compiled.
This is it with cargo! Next we will code up a guessing game in rust.