π¦ variable β Rust Variables
In Rust, variables are used to store data for later use in your program. They are declared using the let keyword.
By default, Rust variables are immutable β meaning once a value is assigned, it cannot be changed. If you want to change the value of a variable later, you must explicitly make it mutable by adding mut before the variable name.
fn main() { let x = 5;//immutable variable println!("x is: {}", x); }
π Line-by-line breakdown:
-
letcreates a new variable. -
xis the variable name. -
5is the integer value assigned. -
println!is a macro that prints text to the console.
π‘ If you're not sure what a macro is, donβt worry! For now, just remember that println! is used to display text in the console.
You might be wondering about the {} inside the string:
This is a placeholder. It gets replaced by the value of x when printed.
So the line:
#![allow(unused)] fn main() { println!("x is: {}", x); }
will output:
#![allow(unused)] fn main() { x is: 5 }