Statements and Variables - Hello, Rust!
Introduction
In the previous chapter, we wrote our first Rust program. In this one, we'll write several more.
Code is typically composed of statements. Statements consist of one or more values and operators. Initialising a variable, setting it to the return value of a function call, even calling a macro (like the println
macro we saw a while ago) are all examples of statements.
Variables
let
let name = "Nivesh";
This is a simple statement that creates a new variable called name
and initialises its value to Nivesh. That's what Sid is short for by the way. Every statement in Rust must end with a semicolon.
let
might seem familiar from JavaScript but is not the same in Rust. In JavaScript, variables created with let
are mutable by default. This is valid JavaScript.
function main() {
let name = "Nivesh";
name = "Saharan";
}
Let's try that in Rust.
fn main() {
let name = "Nivesh";
name = "Saharan";
}
You should see a message when trying to compile this which says:
error[E0384]: cannot assign twice to immutable variable `name`
--> hello-rust.rs:3:5
|
2 | let name = "Nivesh";
| ----
| |
| first assignment to `name`
| help: consider making this binding mutable: `mut name`
3 | name = "Saharan";
| ^^^^^^^^^^^^ cannot assign twice to immutable variable
In Rust, all variables created using let are immutable by default. This means you cannot reassign their value once they've been assigned. That doesn't mean you have to assign them when you declare them.
let name;
name = "Saharan";
This is perfectly valid. You just can't assign them more than once.
mut
If you need a variable that is mutable, you use the mut
keyword when declaring the variable.
fn main() {
let mut name = "Nivesh";
name = "Saharan";
}
The name
variable is now mutable which means you're free to reassign it when you like.
const
Rust also has a const
keyword like JavaScript but again they behave very differently. const
in JavaScript creates a variable with an immutable reference (you can still mutate its value, like pushing to a const Array). In Rust, const creates a compile time constant value.
When you have a true constant whose value is known at compile time, you should use const
. Let's look at an example.
fn main() {
const are_you_cool = true;
println!("{}", are_you_cool);
}
Try compiling and running the code above.
You should see the following error from the Rust compiler:
error: missing type for `const` item
--> hello-rust.rs:2:11
|
2 | const are_you_cool = true;
| ^^^^^^^^^^^^ help: provide a type for the constant: `are_you_cool: bool`
error: aborting due to previous error
Because values initialised with const
are compile time known constants, Rust will not infer their type for you and you will need to provide a type. Let's do that!
fn main() {
const are_you_cool: bool = true;
println!("{}", are_you_cool);
}
This should compile nicely! Now we know two more things: how to type a value in Rust and that you are cool.
Notice that the variable name is snake cased? That's a Rust convention. The Rust people like snake_case for variables but of course it's totally fine to use camelCase. It's a convention, not a rule!