Rust – Introduction and Variables




A systems programming language named Rust is designed to be fast, concurrent, and safe. Presently, it is an open-source programming language that was developed by Mozilla. System programming and the development of low-level libraries and programs benefit greatly from its stringent type-checking, ownership model, and safety-focused approach.

Rust is easy to download and install in any operating system like Windows, macOS, Linux, etc. It has a helpful compiler with appropriate error messages. It offers zero-cost abstraction, allowing us to add abstraction without degrading the functionality of the code. Type inference and safe memory allocation are offered. It offers quick testing and troubleshooting mechanisms. Hence, Rust is a fantastic option for high-performance applications like gaming engines, operating systems, and device drivers. The language is also being adopted by organisations like Amazon, Dropbox, and Microsoft, as well as a wide range of sectors.


In the programming language Rust, "Cargo" refers to the package manager and built-in tool for Rust. It is used to manage dependencies, compile code, and perform other development tasks. With Cargo, developers can easily manage their projects and share their code with others through the package registry. It is also used to build, test, and package Rust applications and libraries.

To create a project:

$ cargo new New-App

Cargo creates a project called New-App. It creates a directory with cargo.toml configuration file and sub-folder src. toml stands for "tom’s obvious minimal language".

To run the project:

$ cargo run

On running the project, it creates a target folder which has the binary files.


In Rust, statements are braced and finished by semicolons. Variables are declared using the let keyword, followed by the variable name, the assignment operator (=), and the value being assigned. For example:

let x = 5;

Variables are immutable by default, meaning their value cannot be changed once assigned. To create a variable that can be reassigned, the mut keyword must be used before the let keyword. For example:

let mut x = 5;

It's also possible to declare a variable with a type annotation. For example:

let x: i32 = 5;

Rust has a strong type system, so the type of a variable must match the type of the value being assigned to it. If a variable is declared without a type annotation, Rust will infer its type based on the value assigned to it. Rust deliberately imitates as much syntax from other languages as it can, particularly C and Python hence it might look familiar.


In Rust, the scope of a variable refers to the portion of the code in which the variable can be accessed. A variable's scope is determined by the location of its declaration and the curly braces that surround it.

A variable that is declared (and enclosed in curly brackets) inside a block of code is only used inside that block of code. For instance:

   let x = 5;
   println!("The value of x is {}", x);
println!("The value of x is not accessible here");

A variable that is defined outside of any code block has a global scope, allowing access to it from any place within the program.

Rust also has a concept of shadowing variables, which allows you to reuse a variable name in the same scope, but the previously-declared variable is no longer accessible, it's being "shadowed" by the new variable.

let x = 5;
let x = x + 1;
let x = x * 2;
println!("The value of x is {}", x);

The above code results in "The value of x is 12". It's important to keep track of the scope of variables in Rust to avoid accidentally accessing a variable outside of its scope, which will cause a compile-time error.

Happy Learning!