Rust Variable Declaration

Posted Under: Programming, Rust, Rust Tutorial For Beginners, Tutorials on May 22, 2022
Rust Variable Declaration
Variable is a symbolic name or reference to an information. They provide a way of labeling data with a descriptive name and provide a way to manipulate information. In Rust, to declare a variable, you use a "let" statement. Also note that Rust is a braced language with statements terminated by semicolons. Its syntax is heavily inspired from C and Python programming language to make it familiar to programmers for easy switching. Now lest declare a variable apples and initialize it to the integer value of 5.
fn main() {
    let apples=5;
Rust is a strongly typed language which mean you have to define the date type of the varibale, but whenever Rust can figure out the appropriate type for you, you can just leave the type annotation out. If you do annotate the type it looks like this
let apples :i32 =5;
: a colon after the variable name, and then the type. "i32", in this case, is a signed 32-bit integer. This is will initialize a single variable appales in Rust. let" can also be uses to initialize multiple variables at once.
let (apples , oranges )=(6,3);
A let statement can de-structure the data on the right hand side and use it to initialize variables inside of a corresponding pattern on the left hand side. Unlike other programming languages variables in Rust are immutable by default, which mean you can not change their values until you make them mutable. This is to achive Rust objectives, Safety, concurrency and speed. There are lots of bugs that can not occure if a value never changes. So safety is improved by immutability. Data that never changes can be shared between multiple threads without locks, so concurrency is improved by immutability. The compiler can also do extra optimizations on data it knows will not change, so speed is improved by immutability. That is why data is immutable by default in Rust: safety, concurrency and speed. But what if we need to we need to change the value of a variable, we need to just add mut
let mut apples =5;
mut makes an immutable varibale mutbale, it value is not changable.

There is also a kind of variable that is even immutabler: the constant. There are four things different about declaring a constant:
const SECONDS_IN_ONE_HOUR :i32=3600;
  1. "const" instead of "let".
  2. The convention is to use SCREAMING_SNAKE_CASE for constants, meaning all uppercase words separated by underscores.
  3. Type annotation is mandatory
  4. The value must be a constant expression that can be determined at compile time.
The Rust team is adding more and more standard library functions to the constant list every release. So there is actually quite a bit of things you can do at compile time that counts as a constant expression. A literal always works just fine. So why would you go to all this trouble to use a const? There is couple of reason for this. One you can place a constant outside of a function at module scope and use it anywhere you want. Two, because const values are inline at compile time, they are really fast!