Your first Rust program
In this section, you will learn how to create and compile your first Rust program using tools such as cargo
.
Anatomy of a Rust program
A basic Rust program is organized as follows:
Cargo.toml
Cargo.lock
src
└── main.rs
target
└── debug
└── myprogram
- The file
Cargo.toml
describes your program (name, version, authors, …). - The file
Cargo.lock
contains information about the version of the other packages used when compiling your program. src
contains the source of your program —main.rs
is the file containing themain
function.target
is a directory created by the compiler; it will contain the output of the compilation, including the executable produced from your source files, indebug/myprogram
(ifmyprogram
is the name of your program as described inCargo.toml
). Thetarget
directory should never be checked in your version control software. It should be ignored completely (e.g., by adding it to your.gitignore
if you are usinggit
).
Creating your first Rust program: intro
Exercise 0.a: Create the skeleton of your first Rust program by using the command cargo new
:
$ cargo new intro
Created binary (application) `intro` package
This will create a intro
directory with a complete skeleton of a Rust program:
$ tree intro
intro
├── Cargo.toml
└── src
└── main.rs
(you might not have the tree
on your computer – never mind, we will not need it)
Change your current directory to intro
and display the content of main.rs
:
$ cd intro
$ cat src/main.rs
fn main() {
println!("Hello, world!");
}
As you can see, the program is pretty basic. You can compile it by using cargo build
:
$ cargo build
Compiling intro v0.1.0 (/tmp/intro)
Finished dev [unoptimized + debuginfo] target(s) in 0.54s
Your program has compiled without errors. You can execute it by using cargo run
:
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/intro`
Hello, world!
Note that you can execute cargo run
without first executing cargo build
: the application will be built automatically.
Editing the Rust program
Exercise 0.b: Using your editor of choice (for example Visual Studio code or NetBeans), edit main.rs
so that the main program now displays a count from 1 to 5 inclusive:
fn main() { for i in 1..=5 { println!("i = {i}"); } }
Ensure that your editor is Rust-aware, or install the necessary extensions if needed. For Visual Studio Code, we suggest the installation of the "rust-analyzer" extension.
You can now execute your code. If everything went well, you should see the following output:
$ cargo run
Compiling intro v0.1.0 (/tmp/intro)
Finished dev [unoptimized + debuginfo] target(s) in 0.20s
Running `target/debug/intro`
i = 1
i = 2
i = 3
i = 4
i = 5
You can also compile your program and run the executable directly. As cargo run
output indicates it is located in target/debug/intro
:
$ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
$ ls -l target/debug/intro
-rwxr-xr-x 2 sam sam 4203904 Jan 16 15:02 target/debug/intro
$ target/debug/intro
i = 1
i = 2
i = 3
i = 4
i = 5