Chapter 1. Introduction to Rust
Introduction
Install Rust
First, install Rust from officical web site. Then you will be able to create your first project. To verify that you installed correctly Rust, you can execute this command and it will return the current version of your Rust:
rustup --version
Update Rust
To update rust to the latest stable version:
rustup update
Official Documentation
To see the official Rust documentation (which one will be best if you want to learn deeper) you have to execute this command:
rustup doc
Getting dependencies for offline work
If you want to get the necessary dependencies to work without internet, you can download and use those later:
# Create a new project with cargo
cargo new get-dependencies
# Go to the project
cd get-dependencies
# Add the needed dependencies
cargo add rand@0.8.5 trpl@0.2.0
First Steps
Creating our first Hello World
Rust can run only with one file, to do this, We will create a directory where you will set your .rs file
Rust works with files which ends in .rs.
In this book we only work with UNIX like systems and Linux, so if you are working on Windows, we recommend you see the documentation in rustup doc or official website.
Something like this:
mkdir firstProject
cd firstProject
In firsProject we will create, using VS Code or another code editor our .rs file. In this case I will create it with Vim:
vim main.rs
If the name of your file has more than one word, the convention indicates that the most correct way to name your file is using an underscore in each word space:
hello_world β
helloworld β
helloWorld β
In your main.rs we have to create our main function, we are going to see this more in the future, and after that, you have to put a println!, it is a Rust macro function, which one we will see forward.
fn main() {
println!("Hello world")
}
To execute this file, we will write in Command Line, in the route where our file is:
rustc main.rs
Hello world
Parts of our first Rust Code
Main Function
This function will be the first part of our Rust program that will be executed, if there is a parameter to be passed, it has to be set inside of ().
Rust Macro
In this case, the macro is println!. If it was a function, it had been called without the !. In this point, just keep in mind that a macro is not the same thing that a function.
Compilation and Execution
Before to run a program, you have to compile it, this task is made by the rust compiler using rustc followed by the name of our Rust file.
rustc main.rs
If you have already use c or c++, you will notice that is something like these. Rust will create a binary file as an output of our program, this is the executable.
ls
main main.rs
To execute this binary or executable file, as others in Linux or MAC, you just have to set this in Command Line:
./main
Rust is Compiled
Rust is an ahead-of-time compiled language, this means that you can compile and reproduce your binary to share with other people, without they have installed Rust in their machines!
Cargo
Cargo is the Rust's build system and package manager. It Handles a lot of task for you:
- Manage all libraries and pull all needed by your code
- Build your code and manage your binary
- Build your own libraries
Cargo is useful in that kind of projects where you handle more complexity. Cargo is already installed with Rust. To verify if it is already installed, you have to execute this:
cargo --version
Create new Project with cargo
To create a new project in rust, you have to run cargo new <project-name>. This command will create a rust project:
cargo new project_name
project_name
|--cargo.toml
|--src
|--main.rs
...
Files and initial code
cargo.toml: This file will contain metadata that will used in the project, like libraries, versions, packages, etc.src/: This will contains al resources that will be used in the project, those can be codes or other files.main.rs: Main code of the program
It has also initialized a new Git repository along with a .gitignore file. Git files wonβt be generated if you run cargo new within an existing Git repository; you can override this behavior by using cargo new --vcs=git.
Note: Git is a common version control system. You can change
cargo newto use a different version control system or no version control system by using the--vcsflag. Runcargo new --helpto see the available options.
Cargo and TOML format
Your cargo.toml file must be something like this:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2024"
[dependencies]
This kind of files is called TOML (Tomβs Obvious, Minimal Language). Cargo is written in this format
Headings
[package]: In this section will be our statements to configure a package. Under this section is the necessary information to compile our program[dependencies]: Here will be the crates that your project need. Crates is the term to refer a dependency of Rust
Build Binaries with cargo
Building
To build a project with cargo you just have to execute the command build in Command Line (Obviously, it has to be executed in the root path of the project):
cargo build
This will build an executable in target/debug/project_name, in this case, project_name is the name of the binary, and it will be the same name of your project name
To execute this binary is:
./target/debug/project_name
Hello, world!
This command will generate the cargo.lock file. This file keeps track of the exact versions of dependencies in your project. AT this moment this project does not have dependencies, so this file is a bit sparse. You wonβt ever need to change this file manually; Cargo manages its contents for you.
Run your first program
cargo run works like cargo build, the difference is that cargo run builds and executes the program, without have to use the complete path to the binary. Most of the developers prefers this command.
If you ran twice this code, you will notice that this time logs of building does not appear, this is because if your code does not have changes, Rust won't see useful rebuilding the program, except if you changed something in the code.
Cargo checking
Cargo also provides a command called cargo check. This command quickly checks your code to make sure it compiles but doesnβt produce an executable.
This option is faster than build the whole project, so many times is more convenient use this command than cargo build or cargo run.
Building for release
When your project is ready to be released, you have a easy way to do this. Use:
cargo build --release
This command will build cargo with some optimizations. This command will create an executable in target/release instead of target/debug. This optimizations will make your code running faster, but turning them on lengthens the time it takes for your program to compile. This is why there are tow different profiles:
- Development: You want to rebuild quickly
- Final or Release: You want to run in the fastest way your programs, without take much time in compilation.
Cargo's Conventions
Maybe now you do not see the relevance of cargo, but once your project will be bigger, with more files and dependencies, it's much easier to let cargo coordinate your project.
You can check out some git project and compile by your own self and execute those:
git clone example.org/someproject
cd someproject
cargo build