Introduction to Rust
Table of content
- Introduction to Rust
- Installing of Rust and Environment setup
- Introduction to Cargo
- cargo new hello_world
- cargo build
- cargo run
- cargo build --release
- cargo clean
- Running Hello world application
- Understanding the code
- code block
- function
- macros
- cargo.toml / cargo.lock
- Debugging using codeLLDB
Introduction to Rust
Rust is a multi-paradigm, general-purpose programming language
designed for performance and safety, especially safe concurrency. Rust
is syntactically similar to C++, but can guarantee memory safety. It was created at Mozilla foundations.
Installing of Rust and Environment setup
Rust is easy to install. The most recommended way to install Rust programming language is through rustup tool.
The command to install rustup tool on linux/mac is :
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
In the Rust development environment, all tools are installed to the
~/.cargo/bin
directory, and this is where you will find the Rust toolchain,
including rustc
, cargo
, and rustup
.
To check the installed rust version run rustc --version command in the terminal to verify rust is installed successfully.
$ rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
$
Introduction to Cargo
Cargo is the Rust package manager. Cargo lets you download crates, compiles your code, make your own crates and upload them to https://crates.io . Crates IO is a centralized crate hosting site. We can check required crates and import them to use in our application.
Below are the common cargo commands :
cargo new <package_name> : to create a new application or package.
cargo build : is used to build and application. The new build is located in the target folder.
cargo run : is used to build and run the application.
cargo build --release : is used to release a production ready application.
cargo clean : is used to clean the previous build and delete the target folder.
To create a new cargo application, run cargo new command with package name.
$ cargo new hello_world
Created binary (application) `hello_world` package
$
cargo new command creates a new rust application structure.
/src folder includes the rust source code,
/target folder contains the builds,
.gitignore file,
Cargo.toml and Cargo.lock file.
Overall structure of the application looks as below.
Running Hello world application
In the /src folder you see a main.rs file. This is the starting point of the application. This file contains the main method. When we run the Rust application, the rust runtime calls the main() function from main.rs file.
fn main() {
println!("Hello, world!");
}
To run the application we can run the cargo run command in terminal in the root folder of the application. In the above example run the command in hello_world folder.
$ cargo run
Compiling hello_world v0.1.0 (/Users/shankernaik/code/rust/absolute-rust/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 1.97s
Running `target/debug/hello_world`
Hello, world!
$
As you can see the cargo compiles the application and run the application with the output as Hello, world!
Understanding the code
Code block in Rust programming language start with a { opening curly bracket and ends with a } closing curly bracket. Code block is used to define lifetime and scope for a variable.
Function in Rust programming language starts with the keyword fn followed by function main. In the hello_world application the main() is a function. It is the starting point of the application. We can define our own functions.
Macros in Rust programming are nothing but precompiled functions. Macros end with a bang character (!). We can define our own macros.In the above example println!() is a macro that prints information to the output stream (Default to console).
cargo.toml is dependency management file. It specifies the name, version and edition of the current application. It also lists down third party crate dependencies with version number if any. Below is the default content of cargo.toml file when a new application is created. As you can see there are no dependencies to the hello_world application.
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]