Skip to content

Latest commit

 

History

History
80 lines (60 loc) · 1.25 KB

declaring_variables.md

File metadata and controls

80 lines (60 loc) · 1.25 KB

Declaring Variables

A variable is a kind of storage that we can put numbers, strings or other data types in it. To declare a variable, we use let. For exmple:

fn main() {
    let n = 1;
    println!("n is {}", n)
}

Output:

n is 1

We can also store a string in a variable.

fn main() {
    let s = "Hello";
    println!("The string is \"{}\".", s);
}

Output:

The string is "Hello".

We can compute and store a result of an expression before we print it out.

fn main() {
    let n = 11 * 19 + 3;
    println!("n = {}", n)
}

Output:

n = 212

If we declare a variable but not use it later as follows,

fn main() {
    let n = 1;
}

we will get a compiler warning when we execute cargo build.

warning: unused variable: `n`
 --> src/main.rs:2:9
  |
2 |     let n = 1;
  |         ^ help: if this is intentional, prefix it with an underscore: `_n`
  |
  = note: `#[warn(unused_variables)]` on by default

We can put _ before the variable name to fix this warning as suggested above.

fn main() {
    let _n = 1;
}

➡️ Next: Specifying Types Of Variables

📘 Back: Table of contents