0
0
Rustprogramming~20 mins

Propagating errors with ? in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Error Propagation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Rust code using the ? operator?

Consider this Rust function that reads a number from a string and doubles it. What will be printed when calling main()?

Rust
fn double_number(s: &str) -> Result<i32, std::num::ParseIntError> {
    let n: i32 = s.parse()?;
    Ok(n * 2)
}

fn main() {
    match double_number("10") {
        Ok(val) => println!("Result: {}", val),
        Err(e) => println!("Error: {}", e),
    }
}
AResult: 20
BResult: 10
CError: invalid digit found in string
DCompilation error due to missing match
Attempts:
2 left
💡 Hint

Remember that ? returns the error early if parsing fails, otherwise continues.

Predict Output
intermediate
2:00remaining
What error does this Rust code raise when using the ? operator?

What happens when running this Rust code?

Rust
fn parse_and_double(s: &str) -> Result<i32, std::num::ParseIntError> {
    let n: i32 = s.parse()?;
    Ok(n * 2)
}

fn main() {
    match parse_and_double("abc") {
        Ok(val) => println!("Result: {}", val),
        Err(e) => println!("Error: {}", e),
    }
}
APanic at runtime due to unwrap on Err
BError: invalid digit found in string
CCompilation error: cannot use ? outside function returning Result
DResult: 0
Attempts:
2 left
💡 Hint

What happens if parsing fails with ? inside a function returning Result?

🔧 Debug
advanced
2:00remaining
Why does this Rust code fail to compile when using the ? operator?

Examine this Rust code snippet. Why does it fail to compile?

Rust
fn try_parse(s: &str) {
    let n: i32 = s.parse()?;
    println!("Number: {}", n);
}

fn main() {
    try_parse("42");
}
Aparse() returns Option, not Result
BMissing semicolon after parse() call
CCannot use ? operator inside main function
DFunction try_parse must return Result to use ? operator
Attempts:
2 left
💡 Hint

The ? operator can only be used in functions that return Result or Option.

📝 Syntax
advanced
2:00remaining
Which option correctly propagates errors using the ? operator in Rust?

Choose the code snippet that correctly uses the ? operator to propagate errors.

A
fn read_number(s: &amp;str) -&gt; Result&lt;i32, std::num::ParseIntError&gt; {
    let n = s.parse()?;
    Ok(n)
}
B
fn read_number(s: &amp;str) -&gt; i32 {
    let n = s.parse()?;
    n
}
C
fn read_number(s: &amp;str) -&gt; Result&lt;i32, std::num::ParseIntError&gt; {
    let n = s.parse().unwrap();
    Ok(n)
}
D
fn read_number(s: &amp;str) -&gt; Result&lt;i32, std::num::ParseIntError&gt; {
    let n = s.parse();
    Ok(n?)
}
Attempts:
2 left
💡 Hint

Remember that ? can only be used on a Result or Option value directly.

🚀 Application
expert
3:00remaining
How many times is the error propagated in this Rust code using multiple ? operators?

Given this Rust code, how many times will the error be propagated if the input string is invalid?

Rust
fn step1(s: &str) -> Result<i32, std::num::ParseIntError> {
    let n = s.parse()?;
    Ok(n)
}

fn step2(s: &str) -> Result<i32, std::num::ParseIntError> {
    let n = step1(s)?;
    Ok(n * 2)
}

fn main() {
    match step2("not_a_number") {
        Ok(val) => println!("Value: {}", val),
        Err(e) => println!("Error: {}", e),
    }
}
AThe error is propagated twice inside step1 before returning
BThe error is caught and handled inside step1, so no propagation
CThe error is propagated once from step1 to step2, then to main
DThe error causes a panic and does not propagate
Attempts:
2 left
💡 Hint

Think about how the ? operator passes errors up the call stack.