Exercises

Exercise 1

Press + to interact
fn main() {
if 5 == 5 {
println!("5 == 5 is true");
}
if 5 > 5 {
println!("5 > 5 is true");
}
if 5 >= 5 {
println!("5 >= 5 is true");
}
}
Q

What is the output of this program?

A)

5 == 5 is true

5 >= 5 is true

B)

5 == 5 is true

5 > 5 is true

C)

5 >= 5 is true

5 > 5 is true

Exercise 2

Replace all four FIXMEs below with the correct type:

Press + to interact
fn want_apples() -> bool {
true
}
fn want_five() -> bool {
false
}
fn talk_about_fruit() -> bool {
true
}
fn talk_about_numbers() -> bool {
false
}
fn main() {
let fruit: FIXME = if want_apples() {
"apple"
} else {
"banana"
};
let number: FIXME = if want_five() {
5
} else {
6
};
let _x: FIXME = if talk_about_fruit() {
println!("The fruit is {}", fruit);
};
let _y: FIXME = if talk_about_numbers() {
println!("The number is {}", number);
};
}

Exercise 3

Rewrite the program below so it doesn’t use any lets. Reminder: you can put any expression inside a macro call’s parameter list.

Press + to interact
fn comment(apples: i32) {
let message = if apples > 10 {
"lots of"
} else {
"very few"
};
println!("You have {} apples", message);
}
fn main() {
comment(5);
comment(100);
}

Exercise 4

...