Exercises

Exercise 1

fn main() {
    x = 5;
    println!("x has the value {}", x);
}
Solution
fn main() {
    let x = 5; // You needed to add a `let` in front of the variable `x`
    println!("x has the value {}", x);
}

Exercise 2

fn main() {
    let x;
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}
Solution
fn main() {
    let x: u8 = 10; // Make sure you intiailize `x`
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}

Exercise 3

fn main() {
    let x: i32;
    println!("Number {}", x);
}
Solution
fn main() {
    let x: i32 = 0; // Make sure you initialize `x`
    println!("Number {}", x);
}

Exercise 4

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; // add the `mut` to make the variable mutable to change it to 5
    println!("Number {}", x);
    x = 5; // don't change this line
    println!("Number {}", x);
}

Exercise 5

fn main() {
    let number = "T-H-R-E-E"; // don't change this line
    println!("Spell a Number : {}", number);
    number = 3; // don't rename this variable
    println!("Number plus two is : {}", number + 2);
}
Solution
fn main() {
    let number = "T-H-R-E-E"; // don't change this line
    println!("Spell a Number : {}", number);
	// Change `number` to a different scope
    {
        let number: u8 = 3; // don't rename this variable
        println!("Number plus two is : {}", number + 2);
    }
}

Exercise 6

const NUMBER = 3;
fn main() {
    println!("Number {}", NUMBER);
}
Solution
const NUMBER: u8 = 3;
fn main() {
    println!("Number {}", NUMBER);
}