Link Search Menu Expand Document

Solution : Defining Variables

fn test() {
    // declare a mutable variable `x`
    let mut x = 1000;
    // declare a variable `y`
    let y="Programming";
    // print output of `x`
    println!("x:{}", x);
    // print output of `y`
    println!("y:{}", y);
    // update x
    x = 1100;
    // print output of `x`
    println!("x:{}", x);
    // print output of `y`
    println!("y:{}", y);
}


Output


x:1000
y:Programming
x:1100
y:Programming

  • Explanation

    • On line 3, a mutable variable x is defined and assigned the value 1000.
    • On line 5, a variable y is defined and assigned the value Programming.
    • On line 7 and line 9, the value of x and y is printed respectively.
    • On line 11, the value of x is updated since x is mutable.
    • On line 13 and line 15, the values of x and y is printed respectively.

Now you have learned the concept of variables, what they are, and how to create them. What if you want to define the type of value that goes inside the variable? now lets learn rustlabs - data types