How things vary in Rust...

Photo by CHUTTERSNAP on Unsplash

How things vary in Rust...

The basics of Rust are somewhat easy to pick up when you're coming from any proper conventional programming language that follows most computer programming principles. In the third installment of the Deciphering Rust series, we'll be looking at statements and variables in Rust and how they are similar to languages like JavaScript and then we'll point out the routes where these two languages start to go their separate ways.

Variables in rust are naturally immutable. This means that any data type assigned to a variable cannot be changed or mutated to another variable later on. Before I go deeper into the dynamics of Variables in Rust, in the last post, I showed you how to run a basic Rust program using this example snippet:

fn main() {
    println!("Hello World, This is a test!");
}

There's something I need to point out about this line of code. Rust follows a convention that languages like C and C++ use. The Main function. The main function is the prime function of any Rust program. It's the function that the compiler calls once it's run which is why It's never openly called in a Rust program. If you come from C or C++ you should already be familiar with this pattern of running a program. Every Rust program must have a main function.

If you look at the code snippet I posted you'll see that the main function has one line of code in it. That line of code is synonymous with what a printf in C, std::cout in C++ and console.log() in JavaScript do. It prints out a line to the console. Unlike, JavaScript the println! keyword is not a function but something called a macro. Macros are usually called like functions but they have an exclamation or bang symbol ! at the end which indicates the difference to Rust.

Now that we've filled that knowledge gap, let's get to Rust variables. As I mentioned previously, variables in Rust are Immutable. They can't be changed and they are written using the let keyword just like JavaScript.

let name = "Sylvester";

if we try to change the value of the name after setting it, Rust gives us this error: // insert error in the code snippet

The reason for Rust's variable natural immutability is to prevent the side effect of state change in a program like bugs that come from other parts of the program or other programs depending on a particular state of that variable which is not available anymore because the variable has been changed.

In comes the Mut.

Then how do I make a variable mutable if it becomes quite necessary in my program you may ask? Rust provides themut keyword in these cases. if we do this:

let mut name = "Sylvester";
name = "Hill"; //change the value of name to Hill

We've given the ability to be mutable to the name variable. Variable creation conventions in Rust aren't so different from other languages. We can initialize an immutable:

let age;
age = 15;

or initialize a mutable too:

let mut country;
country = "Nigeria";

and possibly change it later on

country = "Germany";

If you keenly look, you'll see that Rust infers the type of value assigned to a variable for you. Although it's advised that types should be defined for every value assigned to a variable. We'll cover more on this when we talk about scalars in the next post.

Printing Variable values.

Printing a value assigned to a variable follows the same concept as most programming languages. We use print formatting to print the value assigned to the variable.

fn main() {
    let mut country = "Nigeria";
    println!({}, country);
}

The {} serves as a placeholder for the value that the variable country holds.

Const

Like JavaScript, Rust also comes with a const keyword but that's where the similarities end. While JavaScript's const keyword creates a variable with an immutable reference, Rust's const creates a compile-time constant value.

For example:

fn main() {
    const are_you_learning = true;
    println!("{}", are_you_learning);
}

if we run this, we get an error that tells us to provide a type for the constant. This is because values initialized with const are compile-time known constants. Rust prefers to not infer their type for them and requires that a type is provided.

Other Variable creation conventions and rules

By Convention, Rust variables are written in snake case. Variables in rust cannot be started with a number or a symbol but a letter first. Also, variables cannot have symbols or spaces between them, only numbers and letters.