Cargo is a Rust package manager. It is used to manage dependencies and build Rust projects.
We will create a new project called hello-world-cargo
, to do this, we will use the following command.
βΉοΈ the
--bin
parameter flags the project as an application, not a library.
$ cargo new hello-world-cargo --bin
> Created binary (application) `hello-world-cargo` package.
This command created a new folder π hello-world-cargo
in the current directory.
This folder contains a π Cargo.toml
file, a π src
folder and a π main.rs
file.
hello-world-cargo
βββ Cargo.toml
βββ .git
β βββ ...
βββ .gitignore
βββ src
βββ main.rs
It also initialized a git repository for the project.
The π src
folder contains the source code of the application, in it there already is a π main.rs
file containing an hello world program.
// π main.rs
fn main() {
println!("Hello, world π");
}
File | Description |
---|---|
π Cargo.toml |
The Cargo manifest file, this file contains all the dependencies and the application name. |
π src |
The source folder, this folder contains the source code of the application. |
π main.rs |
The main file, this file contains the source code of the application. |
π .git |
The git folder, this folder contains all the git files, you can ignore this folder if you don't use git. |
π .gitignore |
The git ignore file, this file contains all the files that should be ignored when committing. |
To compile the program, we will use the cargo build
command.
# hello-world-cargo π
$ cargo build
This command will compile the program and create an executable file called π hello-world-cargo
in the new π target/debug
folder.
# hello-world-cargo/target/debug π
$ ./hello-world-cargo
> Hello, world π
To compile and run the program, we will use the cargo run
command.
# hello-world-cargo π
$ cargo run
...
> Hello, world π
Home π - Next Section βοΈ