0
0
Rustprogramming~10 mins

Result enum in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AErr
BSome
COk
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using Err instead of Ok for success
Using Option variants like Some or None
2fill in blank
medium

Complete 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'
AOk
BNone
CSome
DErr
Attempts:
3 left
💡 Hint
Common Mistakes
Matching Err instead of Ok for success
Using Option variants in match
3fill in blank
hard

Fix 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'
Aunwrap
Bunwrap_or
Cexpect
Dunwrap_err
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap which panics on error
Using unwrap_err which unwraps the error variant
4fill in blank
hard

Fill 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'
AErr
BOk
DSome
Attempts:
3 left
💡 Hint
Common Mistakes
Using Ok to create an error
Matching Ok instead of Err for errors
5fill in blank
hard

Fill 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'
Amap
B*
CErr
DOk
Attempts:
3 left
💡 Hint
Common Mistakes
Using map_err instead of map
Using + instead of * for multiplication
Matching Ok instead of Err for errors