Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to match on the Result and print "Success" if Ok.
Rust
let result: Result<i32, &str> = Ok(10); match result { [1] => println!("Success"), Err(e) => println!("Error: {}", e), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Ok without parentheses causes a syntax error.
Using Ok(10) only matches if the value is exactly 10.
✗ Incorrect
Using Ok(_) matches any successful result regardless of the value inside.
2fill in blank
mediumComplete the code to extract the error message from Err variant.
Rust
let result: Result<i32, &str> = Err("Failed"); match result { Ok(val) => println!("Value: {}", val), [1] => println!("Error: {}", err), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Binding to a variable name different from the one used in println! causes errors.
Using Err without a variable name causes a syntax error.
✗ Incorrect
Binding the error to 'err' allows printing it in the println! macro.
3fill in blank
hardFix the error in the match arms to handle both Ok and Err correctly.
Rust
let result: Result<i32, &str> = Ok(5); match result { Ok(value) => println!("Value is {}", value), [1] => println!("Error occurred"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Err without parentheses causes syntax errors.
Using Err(e) without using 'e' causes warnings.
✗ Incorrect
Err(_) matches any error value without binding it, avoiding unused variable warnings.
4fill in blank
hardFill both blanks to match on Result and print the value or error message.
Rust
let result: Result<&str, &str> = Err("Not found"); match result { [1] => println!("Found: {}", val), [2] => println!("Error: {}", err), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in match arms and println! causes errors.
Not binding variables causes inability to print values.
✗ Incorrect
Ok(val) binds the success value to val; Err(err) binds the error to err for printing.
5fill in blank
hardFill all three blanks to create a match that handles Ok with a number, Err with a message, and a default case.
Rust
let result: Result<i32, &str> = Ok(42); match result { [1] => println!("Number: {}", num), [2] => println!("Error: {}", msg), [3] => println!("Unknown result"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Err(e) instead of Err(msg) when msg is used in println! causes errors.
Omitting the default case causes match to be non-exhaustive.
✗ Incorrect
Ok(num) binds the number; Err(msg) binds the error message; _ matches any other case as default.