Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Result that holds a success value of 10.
Rust
let result: Result<i32, &str> = [1](10);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Err instead of Ok for success
Using Option variants like Some or None
✗ Incorrect
The Ok variant of Result represents success and holds the value.
2fill in blank
mediumComplete the code to handle a Result and print the success value.
Rust
match result {
[1](val) => println!("Success: {}", val),
Err(e) => println!("Error: {}", e),
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Matching Err instead of Ok for success
Using Option variants in match
✗ Incorrect
When matching a Result, the success case is Ok.
3fill in blank
hardFix the error in the code to unwrap the Result safely.
Rust
let value = result.[1](0);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap which panics on error
Using unwrap_err which unwraps the error variant
✗ Incorrect
unwrap_or returns the success value or a default if it's an error.
4fill in blank
hardFill both blanks to create a Result and handle the error case.
Rust
let result: Result<i32, &str> = [1]("Failed"); match result { Ok(val) => println!("Value: {}", val), [2](e) => println!("Error: {}", e), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Ok to create an error
Matching Ok instead of Err for errors
✗ Incorrect
Err creates an error Result and matches the error case.
5fill in blank
hardFill all three blanks to map a Result's success value and handle errors.
Rust
let result: Result<i32, &str> = Ok(5); let new_result = result.[1](|x| x [2] 2); match new_result { Ok(val) => println!("Doubled: {}", val), [3](e) => println!("Error: {}", e), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using map_err instead of map
Using + instead of * for multiplication
Matching Ok instead of Err for errors
✗ Incorrect
map applies a function to the success value, * multiplies, and Err matches errors.