-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
20 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
One of the most loved features of Rust is the way it lets you handle errors. The `Result` type is a powerful tool that allows you to handle errors in a way that is both safe and expressive. In this challenge, you will be working with the `Result` type to handle errors in a graceful way. | ||
One of the most loved features of Rust is the way it lets you handle errors. The `Result` type is a powerful tool that allows you to handle errors in a way that is both safe and expressive. In this challenge, you will be working with the `Result<T, E>` type to handle errors in a graceful way. | ||
|
||
## Task | ||
The `Result<T, E>` itself is an enum that has two variants: `Ok(T)` and `Err(E)`. The `Ok` variant is used to represent a successful computation that returns a value of type `T`. The `Err` variant is used to represent an error that returns a value of type `E`. | ||
|
||
When you have a function that can fail, you can use the `Result` type to return the result of the computation. If the computation is successful, you can return the success variant of `Result` with the value of the computation. If the computation fails, you can return the error variant of `Result` with an error message that explains what went wrong. | ||
|
||
## Your task | ||
|
||
In this challenges, you're given a function, `parse_percentage(input: &str) -> Result<u8, String>` that takes a string as input and returns a `Result` type. The function should parse the input string as a percentage and return the percentage as a `u8` if the input is valid. If the input is invalid, the function should return an error message as a `String`. | ||
|
||
Parsing from a string to a number can fail for many reasons. For example, the input string may not be a valid number, or it may be a valid number but not a valid percentage. Your task is to handle these errors gracefully and return an error message that explains what went wrong. | ||
|
||
Complete the function, if the function returns an `Ok` value, print the value to the console. If the function returns an `Err` value, print the error message to the console. | ||
Complete the function, if the parsing was successful return a success variant of the `Result`, if there was an error in parsing, return an error variant of the `Result` with an error message. | ||
|
||
## Requirements | ||
|
||
- If the parse was successful, the function should return the success variant of `Result` with the percentage as a `u8`. | ||
- If the number was out of range (not between 0 and 100), the function should return the error with `String` "Percentage out of range". | ||
- If the string was not a valid number, the function should return the error with `String` "Invalid input". | ||
|
||
## Example | ||
|
||
```rust | ||
let result = parse_percentage("50"); | ||
assert_eq!(result, Ok(50)); | ||
|
||
let result = parse_percentage("101"); | ||
assert_eq!(result, Err("Percentage out of range".to_string())); | ||
|
||
let result = parse_percentage("abc"); | ||
assert_eq!(result, Err("Invalid input".to_string())); | ||
``` |