Table of Contents๐
Hello world with cargo๐ข
What is cargoโ
Cargo is a Rust package manager. It is used to manage dependencies and build Rust projects.
Creating a new project๐
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. |
Compiling and running a program with cargo๐
Just compiling
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 ๐
Compiling and running๐
To compile and run the program, we will use the cargo run
command.
# hello-world-cargo ๐
$ cargo run
...
> Hello, world ๐
Home ๐ - Next Section โญ๏ธ