Getting Started With Rust
A small introduction to the Rust Programming Language.
Rust is a systems programming language developed at Mozilla, it has a syntax similar to C++ has performance comparable to C++. Rust has ranked as the most loved programming language for the seventh year in a row. In this article, we will see how to write our first Rust program along with some basics.
Installation
The installation of the Rust toolchain is very easy thanks to rustup, an easy CLI tool to manage rust installations.
You can download the rust toolchain for your specific platform from here.
To verify the installation, run rustc --version
from your shell. You should see an output like this. Of course, your version could be different.
Writing Our First Rust Program
Create a main.rs
file in the directory of your choice and open it with your favourite editor and write our very first hello world program.
fn main() {
println!("Hello, World!");
}
To run the program, run rustc main.rs
and ./main
from your shell.
You should see Hello, World!
printed on the console. Okay, let's break down what we just did.
First, in our main.rs
file we declared the main function with the fn
keyword, which is the entry point to our program. The line println!("Hello, World!");
is a macro which logs the formatted string to stdout.
That's it, that's our rust hello world program, which we compile and run through manual rustc invocation, but it's very simple so let's dive deeper in the next section.
Cargo and Editor Setup
Cargo is a package manager used to compile, run and manage dependencies for rust projects and is included in the rust installation by default, so from now on we will use cargo for working with the rust compiler instead of directly invoking it. We should also set up VS Code with rust-analyzer extension for rust language support in the editor.
Once set up, go ahead into your favourite directory and run cargo new hello_rust
, it will create a new cargo project with a Cargo.toml
file for project-specific metadata and external dependencies. The src
folder contains the main.rs
file, which has the main
function for our program.
Inside the hello_rust directory, run cargo run
and you should see Hello, World!
printed on the console. Under the hood, cargo compiles the package with rustc and invokes the executable located at target/debug/.
Variables
In rust, variables are declared using the let
keyword and are immutable by default, so the following code will not compile yet.
fn main() {
let x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x now is: {}", x);
}
You will see a compiler error with an error message and help to remove the error.
To fix the error, we will make x mutable:
fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x now is: {}", x);
}
It will now run successfully and print both the values of x.
Data Types
Integers
There are mainly two integer types in rust, unsigned (u32
, u64
, u128
) and signed (i32
, i64
, i128
).
By default, any integer variable without explicit type declaration is inferred as i32
by the rust compiler. To specify the type, we explicitly define the data type of the variable.
fn main() {
let x: u32 = 5;
println!("The value of x is: {}", x);
}
Floating Point Numbers
There are two floating-point types, f32
and f64
.
Booleans
In rust, booleans are called "bool" and can have values true
or false
. An example would be let is_rust_lovely = true;
.
Character
The char
type is written within single quotes and is any valid Unicode character. An example would be let a = 'a';
That's our gentle introduction to Rust Programming Language which goes over writing our very first rust program and learning some basics like variables and data types, some other complex types like string, structs, enum have been omitted but maybe will write about them in upcoming articles. Also, this is my first blog, so do leave your feedback as it will help me improve my writing skills. Thanks for reading.