Rust basics

Quiz

What you learned

Rust basics quiz. 12 questions. You need 80% to pass.

1.

What does this print?

fn main() {
    let name = 5;
    {
        let name = name + 1;
        println!("inner {name}");
    }
    println!("outer {name}");
}
2.

let wide: i32 = 300. What are these two values?

let truncated = wide as u8;
let too_big: Result<u8, _> = wide.try_into();
3.

What does this print?

fn main() {
    let x: i32 = 7 / 2;
    println!("{x}");
}
4.

This function does not compile. Why?

fn area(width: i32, height: i32) -> i32 {
    width * height;
}
5.

Which loop prints 5, then 4, then 3, then 2, then 1?

for n in 5..1 {}
for n in (1..=5).rev() {}
6.

The loops print 0,0 then 0,1 then 0,2 then 1,0 and stop. Which break does that?

'outer: for x in 0..3 {
    for y in 0..3 {
        if x == 1 && y == 1 {
            break 'outer;
        }
        println!("{x},{y}");
    }
}
7.

What happens with this constant index?

fn main() {
    let numbers = [10, 20, 30, 40, 50];
    println!("{}", numbers[10]);
}
8.

v has 3 elements. What is v.get(9)?

9.

You insert Grace, then Ada, and print every pair. Which map prints Ada before Grace every time?

10.

What does this print?

fn main() {
    let text = "héllo";
    println!("{} {}", text.len(), text.chars().count());
}
11.

A match on an i32 has arms for 1 and 2 and no _. A PHP match has the same two arms and no default. The value is 3. What happens?

12.

age is None. What does this do?

let Some(years) = age else {
    return;
};
println!("{years}");
13.

Will this compile?

fn main() {
    let mut n = 1;
    let r = &n;
    n += 1;
    println!("{r}");
}
14.

What does this print?

fn main() {
    let v = vec![1, 2, 3];
    let n: usize = v.iter().map(|x| x * 2).filter(|x| *x > 2).count();
    println!("{n}");
}

← Back to section