Variables

Additional Resources

Exercises

Declaration

// Fix the variable definition of 'x'

fn main() {
    x = 5;
    println!("x has the value {}", x);
}
Solution
fn main() {
  let x = 5;
  println!("x has the value {}", x);
}

Declaration with reassignment

// Fix the variable definition of 'x'

fn main() {
    let x = 3;
    println!("Number {}", x);
    x = 5; // don't change this line
    println!("Number {}", x);
}
Solution
fn main() {
  let mut x = 3;
  println!("Number {}", x);
  x = 5; // don't change this line
  println!("Number {}", x);
}

Declaration with shadowing

// Fix this code with shadowing

fn main() {
    let x = "three"; // don't change this line
    println!("Spell a Number : {}", x);
    x = 3; // don't rename this variable
    println!("Number plus two is : {}", x + 2);
}
Solution
fn main() {
  let x = "three"; // don't change this line
  println!("Spell a Number : {}", x);
  let x = 3; // don't rename this variable
  println!("Number plus two is : {}", x + 2);
}