With the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if count { }the condition must be a bool, and count is an i32
Practice 04
Read each program and pick an answer. Some programs do not compile, and that can be the right answer. Your choice is checked as soon as you pick it, and saved in this browser.Need a refresher? Read lesson 4.
0 of 50 answered·0 correct
boolWith the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if count { }the condition must be a bool, and count is an i32
With the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if ready { }The condition is a bool, so if accepts it.
With the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if count > 0 { }The condition is a bool, so if accepts it.
With the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if !n { }! on a u8 flips its bits and gives another u8, not a bool
With the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if n == 0 && ready { }The condition is a bool, so if accepts it.
With the declarations below, does this line compile?
let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;
if count && ready { }&& needs a bool on both sides, and count is an i32
What does this print?
let grade = if 72 >= 50 { "pass" } else { "fail" };
println!("{grade}");Running it prints:
pass
n is 3u8. What does this print?
match n {
0 => println!("zero"),
1 | 2 => println!("small"),
_ => println!("big"),
}Running it prints:
big
score is 64u8. What does this print?
match score {
0..=49 => println!("fail"),
50..=64 => println!("pass"),
_ => println!("merit"),
}Running it prints:
pass
n is 0u8. What does this print?
match n {
0 => println!("zero"),
n if n % 2 == 0 => println!("even"),
_ => println!("odd"),
}Running it prints:
zero
This does not compile. What is missing?
match n {
0 => println!("zero"),
1 => println!("one"),
}The compiler says:
error[E0004]: non-exhaustive patterns
a is true and b is false. What does this print?
match (a, b) {
(true, true) => println!("both"),
(true, false) => println!("only a"),
(false, true) => println!("only b"),
(false, false) => println!("neither"),
}Running it prints:
only a
Which arm makes this match cover every value of flag?
let flag = true;
match flag {
true => println!("on"),
}A bool has two values. _ => ... would also work.
What does this print?
let n = 5u8;
let target = 3u8;
match n {
target => println!("matched {target}"),
_ => println!("no match"),
}Running it prints:
matched 5
What does this print?
let n = 2u8;
match n {
1 | 2 | 3 if n > 2 => println!("a"),
_ => println!("b"),
}Running it prints:
b
What does this print?
let score = 55u8;
match score {
0..=100 => println!("in range"),
50..=60 => println!("middle"),
_ => println!("out"),
}Running it prints:
in range
What does this print?
let n = 4u8;
match n {
x if x % 3 == 0 => println!("three"),
x if x % 2 == 0 => println!("two"),
_ => println!("other"),
}Running it prints:
two
What does this print?
let n = 13u8;
match n % 3 {
0 => println!("zero"),
1 => println!("one"),
_ => println!("two"),
}Running it prints:
one
What does this print?
let n = 12u8;
match n & 1 {
0 => println!("even"),
_ => println!("odd"),
}Running it prints:
even
What does this print?
let n = 5u8;
match n << 1 {
10 => println!("ten"),
_ => println!("other"),
}Running it prints:
ten
What does this print?
let n = 0u8;
match !n {
255 => println!("all ones"),
_ => println!("other"),
}Running it prints:
all ones
The same expression, on a signed type. What does this print?
let n = 0i8;
match !n {
-1 => println!("minus one"),
_ => println!("other"),
}Running it prints:
minus one
The same !n on an i8, matched against 255. What does this print?
let n = 0i8;
match !n {
255 => println!("all ones"),
_ => println!("other"),
}The compiler says:
error: literal out of range for `i8`
What does this print?
let n = 7u8;
match n {
x @ 1..=9 if x % 2 == 1 => println!("odd digit {x}"),
x @ 1..=9 => println!("even digit {x}"),
x => println!("not a digit {x}"),
}Running it prints:
odd digit 7
What does this print?
let a = false;
let b = true;
match (a, b) {
(_, true) => println!("b is true"),
(true, _) => println!("a is true"),
_ => println!("neither"),
}Running it prints:
b is true
What does this print?
let p = (1u8, (2u8, 3u8));
match p {
(1, (2, x)) => println!("x is {x}"),
_ => println!("other"),
}Running it prints:
x is 3
This prints two lines. Write both.
let n = 10u8;
match 3u8 {
n if n > 5 => println!("big {n}"),
n => println!("small {n}"),
}
println!("{n}");Running it prints:
small 3 10
This has no _ arm. Does it compile, and what does it print?
let t = -3i8;
match t {
-128..=-1 => println!("below zero"),
0 => println!("zero"),
1..=127 => println!("above zero"),
}Running it prints:
below zero
In a pattern, 0..5 means 0 up to 5, not including 5. What does this print?
let n = 3u8;
match n {
0..5 => println!("under five"),
_ => println!("other"),
}Running it prints:
under five
Does this compile?
let n = 5u8;
match n {
0..=255 => println!("all"),
}Running it prints:
all
Does this compile?
let n = 5u8;
match n {
0..=254 => println!("most"),
}The compiler says:
error[E0004]: non-exhaustive patterns: `u8::MAX` not covered
A bool has two values, and both are written here. Does this compile?
let flag = true;
match flag {
true if 1 > 0 => println!("on"),
false => println!("off"),
}The compiler says:
error[E0004]: non-exhaustive patterns: `true` not covered
Does this compile?
let n = 5u8;
match n {
1..=3 => println!("low"),
4..=6 => println!("mid"),
}The compiler says:
error[E0004]: non-exhaustive patterns: `0_u8` and `7_u8..=u8::MAX` not covered
Does this compile?
let n = 1u8;
let label = match n {
0 => "zero",
_ => 1,
};The compiler says:
error[E0308]: `match` arms have incompatible types
What does this print?
let n = 1u8;
match n {
_ => println!("anything"),
1 => println!("one"),
}Running it prints:
anything
What does this print?
let n = 2u8;
let step = match n {
0 => 1u8,
1 | 2 => 2,
_ => 4,
};
println!("{}", step << 2);Running it prints:
8
What does this print?
let n = 3u8;
if let 0 = n {
println!("zero");
} else {
println!("not zero");
}Running it prints:
not zero
What does this print?
let mut i = 0;
while i < 3 {
println!("{i}");
i += 1;
}Running it prints:
0 1 2
What does this print?
for n in 2..5 {
println!("{n}");
}Running it prints:
2 3 4
What does this print?
for n in 2..=5 {
println!("{n}");
}Running it prints:
2 3 4 5
What does this print?
let mut total = 0;
for n in 1..=4 {
total += n;
}
println!("{total}");Running it prints:
10
What does this print?
let mut count = 0;
loop {
count += 2;
if count > 5 {
break;
}
}
println!("{count}");Running it prints:
6
What does this print?
for n in 0..5 {
if n % 2 == 0 {
continue;
}
println!("{n}");
}Running it prints:
1 3
What does this print?
let mut n = 10;
while n > 10 {
println!("{n}");
n -= 1;
}
println!("done");Running it prints:
done
What does this print?
let marks = [70, 80, 90];
for (i, m) in marks.iter().enumerate() {
println!("{i} {m}");
}Running it prints:
0 70 1 80 2 90
What does this print?
let mut n = 1;
let result = loop {
n *= 2;
if n > 20 {
break n;
}
};
println!("{result}");Running it prints:
32
What does this print?
'outer: for a in 1..=3 {
for b in 1..=3 {
if a * b == 4 {
break 'outer;
}
println!("{a} {b}");
}
}Running it prints:
1 1 1 2 1 3 2 1
Does this compile? If it does, what does it print?
let marks = [70, 80, 90];
for m in marks {
println!("{m}");
}
println!("{}", marks.len());Running it prints:
70 80 90 3
Does this compile? If it does, what does it print?
for n in 0..3 {
println!("{n}");
}
println!("{n}");The compiler says:
error[E0425]: cannot find value `n` in this scope
When does this stop?
let mut i = 0;
while i < 3 {
println!("{i}");
}Nothing in the loop body adds to i, so i < 3 stays true.