Getting started

Quiz

What you learned

Getting started quiz. 13 questions. You need 80% to pass.

1.

Which program compiles? First:

fn main() {
    let mut v = vec![1, 2, 3];
    let r1 = &v;
    println!("{r1:?}");
    let r2 = &mut v;
    r2.push(4);
}

Second:

fn main() {
    let mut v = vec![1, 2, 3];
    let r1 = &v;
    let r2 = &mut v;
    println!("{r1:?}");
}
2.

What does this print?

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

Same function, two files. What does each call do?

<?php
function add(int $a, int $b): int
{
    return $a + $b;
}
echo add('5', 3);
<?php
declare(strict_types=1);
function add(int $a, int $b): int
{
    return $a + $b;
}
echo add('5', 3);
4.

What versions can this requirement resolve to?

[dependencies]
serde = "1"
5.

Cargo.toml gained a crate and Cargo.lock was not updated. What does this command do?

cargo build --locked
6.

The user presses Ctrl-D at the prompt. What does Ok(0) mean here?

let bytes = io::stdin()
    .read_line(&mut line)
    .expect("stdin should be readable");
if bytes == 0 {
    return None;
}
7.

You are in the hello-cargo crate. Where is the binary after this command?

cargo build --release
8.

Which file makes every laptop and CI use the same compiler for this repo?

[toolchain]
channel = "1.94"
components = ["rustfmt", "clippy"]
9.

The cursor should stay on the prompt line, before the program reads. Which call needs an explicit flush, and why?

print!("First number: ");
println!("First number: ");
10.

What does this print?

fn main() {
    let n = 7i32.checked_div(0);
    println!("{n:?}");
}
11.

You are fixing a type error and do not need to run the program. Which command does less work?

cargo check
cargo build
12.

You ran cargo add --dev pretty_assertions. Where can that crate be used?

[dev-dependencies]
pretty_assertions = "1"
13.

You need nightly for one crate that uses an unstable feature. Which command is the right habit?

14.

Will this compile?

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

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