Let's improve previous example a little bit by returning a custom error type instead of a plain string. This will allow us to define specific error types and provide more structured error handling.
The logic of the function remains the same as the previous challenge, but the returned error type is what you need to change.
Define an enum ParsePercentageError
with the following variants:
InvalidInput
: for inputs that cannot be parsed as numbers.OutOfRange
: for numbers that are not in the range 0-100.Implement the Error
trait for ParsePercentageError
. Use the std::error::Error
trait and provide human-readable descriptions for each error.
Update the parse_percentage
function to:
Ok(u8)
if the input is a valid percentage (between 0 and 100).Err(ParsePercentageError::OutOfRange)
if the number is out of range.Err(ParsePercentageError::InvalidInput)
if the input is not a valid number.std::error::Error
trait requires implementing the Display
and Debug
traits. You can derive Debug
by #[derive(Debug)]
and implement Display
manually.std::fmt
module to implement Display
for the error enum, which is required for the Error
trait.// 1. Finish the definitionuse std::fmt::{Display, Formatter};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let x = input.parse::<u8>(); if x.is_err() { return Err(ParsePercentageError::InvalidInput); } if x.clone().unwrap() > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(x.clone().unwrap());}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::fmt::{Display, Formatter};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(val) = String::from(input).parse::<i32>() { if val >= 0 && val <= 100 { return Ok(val as u8); } return Err(ParsePercentageError::OutOfRange); } Err(ParsePercentageError::InvalidInput)}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error; use std::fmt ;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput , OutOfRange , }// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::OutOfRange)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::InvalidInput)") } } }impl Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let num = input.parse::<u8>(); match num { Ok(num ) => { if num <= 100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } } , Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;// 1. Define the ParsePercentageError enum#[derive(Debug,PartialEq )]pub enum ParsePercentageError { InvalidInput, // For inputs that cannot be parsed as numbers OutOfRange, // For numbers that are not in the range 0-100}// 2. Implement the `Error` trait for ParsePercentageErrorimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl std::error::Error for ParsePercentageError {}// 3. Update the parse_percentage functionpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // Try to parse the input string into a u8 let number: u8 = input .parse() .map_err(|_| ParsePercentageError::InvalidInput)?; // Check if the number is within the valid range (0-100) if number > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(number) // Return the parsed percentage if it's valid}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::OutOfRange)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::InvalidInput)") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let result = input.parse::<u8>(); match result { Result::Ok(num) => { if num > 100 { return Result::Err(ParsePercentageError::OutOfRange); } return Result::Ok(num); } Result::Err(_) => { return Result::Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt::{Display , Debug , Formatter};use std::fmt;// 1. Finish the definition#[derive(Debug)]#[derive(PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage must be between 0 and 100"), } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(val) => { if val > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(val); } Err(_) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::OutOfRange)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::InvalidInput)") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let result = input.parse::<u8>(); match result { Result::Ok(num) => { if num > 100 { return Result::Err(ParsePercentageError::OutOfRange); } else { return Result::Ok(num); } }, Result::Err(_) => { return Result::Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
#[derive(Debug, Clone, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::error::Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "invalid input"), ParsePercentageError::OutOfRange => write!(f, "number is out of range"), } }}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num = input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput)?; match num <= 100 { true => Ok(num), false => Err(ParsePercentageError::OutOfRange), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input"), ParsePercentageError::OutOfRange => write!(f, "Out Of Range") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let input_int: Result<u8, _> = input.parse(); match input_int { Ok(res) if res <= 100 => Ok(res as u8), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }} // Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt::Display;// 1. Finish the definition#[derive(Debug)]#[derive(PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage is out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // Implement here match input.parse::<u8>() { Ok(num) => { if num > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(num) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl std::error::Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "not an integer"), ParsePercentageError::OutOfRange => write!(f, "must be 0 to 100") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let Ok(num) = input.trim().parse::<u8>() else { return Err(ParsePercentageError::InvalidInput) }; if num <=100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) }}
use std::{fmt};use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let output = match self { Self::InvalidInput => "not a valid number", Self::OutOfRange => "not a valid percentage" }; write!(f, "{}", output) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let output = match self { Self::InvalidInput => "not a valid number", Self::OutOfRange => "not a valid percentage" }; write!(f, "{}", output) }}// 2. Implement the `Error` traitimpl error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if (0..=100).contains(&n) => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt::{self, Display};#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let output = match self { Self::InvalidInput => "InvalidInput", Self::OutOfRange => "OutOfRange", }; write!(f, "{}", output) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` trait//// Ok how the hell am I supporsed to know all of this.impl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "oopsie") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) => { if n > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(n) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(v) = input.parse::<u8>() { if v <= 100 { Ok(v) } else { Err(ParsePercentageError::OutOfRange) } } else { Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(v) = input.parse::<u8>() { if v <= 100 { Ok(v) } else { Err(ParsePercentageError::OutOfRange) } } else { Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Oh no, something bad went down") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(0..=100) => Ok(input.parse().unwrap()), Ok(x) if !(0..=100).contains(&x) => Err(ParsePercentageError::OutOfRange), _ => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(v) if v <= 100 => Ok(v), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ OutOfRange, InvalidInput}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}/* impl Error for ParsePercentageError { fn provide<'a>(&'a self, request: &mut Request<'a>) { request .provide_ref::<MyBacktrace>(&self.backtrace); } fn provide<'a>(&'a self, request: &mut Request<'a>) { request .provide_ref::<ParsePercentageError>(&self); }} */impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Use `self.number` to refer to each positional data point. write!(f, "{}", self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let number: Option<u8> = input.trim().parse().ok(); match number { Some(num) => { if num <= 100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } }, None => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt::{Debug, Display, Formatter};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { }impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let parsed_int_result = input.parse::<u8>(); if let Ok(parsed_int) = parsed_int_result { if parsed_int <= 100 { Ok(parsed_int) } else { Err(ParsePercentageError::OutOfRange) } } else { Err(ParsePercentageError::InvalidInput) }}
use std::fmt::{Debug, Display, Formatter};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { }impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "Custom error") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let parsed_int_result = input.parse::<u8>(); if let Ok(parsed_int) = parsed_int_result { if parsed_int <= 100 { Ok(parsed_int) } else { Err(ParsePercentageError::OutOfRange) } } else { Err(ParsePercentageError::InvalidInput) }}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input"), ParsePercentageError::OutOfRange => write!(f, "Out of Range"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) => { if (0..=100).contains(&num) { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => { Err(ParsePercentageError::InvalidInput) } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => { write!(f, "Err(ParsePercentageError::InvalidInput)") } ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|x| { (0..=100) .contains(&x) .then_some(x) .ok_or(ParsePercentageError::OutOfRange) })}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f,"Invalid input"), ParsePercentageError::OutOfRange => write!(f,"Input out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(num) if num > 100 => Err(ParsePercentageError::OutOfRange), Ok(num) => Ok(num), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let message = match self { ParsePercentageError::InvalidInput => "InvalidOutput", ParsePercentageError::OutOfRange => "OutOfRange", }; write!(f, "{}", message) }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let percentage = match input.parse::<u8>() { Ok(value) => value, Err(_) => return Err(ParsePercentageError::InvalidInput), }; if percentage > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(percentage)}
use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::InvalidInput)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(result) = input.parse::<u8>() { if result > 100 || result < 0 { return Err(ParsePercentageError::OutOfRange) } else { return Ok(result) } } else { return Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self{ ParsePercentageError::InvalidInput=>write!(f, "Err(ParsePercentageError::InvalidInput)"), ParsePercentageError::OutOfRange=>write!(f, "Err(ParsePercentageError::OutOfRange)") } }}impl std::error::Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num:i32=input.parse().map_err(|_| ParsePercentageError::InvalidInput)?; if num>100 || num<0{ return Err(ParsePercentageError::OutOfRange); } return Ok(num as u8);}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error; use std::fmt; // 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "invalid number."), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ Ok(number) if number > 100 => Err(ParsePercentageError::OutOfRange), Ok(number) => Ok(number), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error; use std::fmt; // 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "invalid number."), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ Ok(number) if number > 100 => Err(ParsePercentageError::OutOfRange), Ok(number) => Ok(number), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error; use std::fmt; // 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number."), ParsePercentageError::OutOfRange => write!(f, "Number is out of the allowed range (0-100)."), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ Ok(number) if number > 100 => Err(ParsePercentageError::OutOfRange), Ok(number) => Ok(number), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error; use std::fmt; // 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number."), ParsePercentageError::OutOfRange => write!(f, "Number is out of the allowed range (0-100)."), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ Ok(number) if number > 100 => Err(ParsePercentageError::OutOfRange), Ok(number) => Ok(number), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{ write!(f, "Error") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse(){ Ok(n @ 0..=100) => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{ write!(f, "Error") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse(){ Ok(n @ 0..=100) => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { fn description(&self) -> &str { match self { ParsePercentageError::InvalidInput => { return "Invalid input." } ParsePercentageError::OutOfRange => { return "Out of range." } } }}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let percentage: Result<u8, _> = input.parse(); println!("parse_percentage: {:?}", percentage); match percentage { Ok(pct) => { if pct > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(pct); } Err(_e) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value @ 0..=100) => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError { fn source(&self) -> Option<&(dyn Error + 'static)> { None }}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Error") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Ok(n @ 0..=100) => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}pub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Invalid input or percentage out of range") }}impl Error for ParsePercentageError { fn source(&self) -> Option<&(dyn Error + 'static)> { None }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let percentage = input.parse::<u8>(); match percentage { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(percentage) if percentage > 100 => Err(ParsePercentageError::OutOfRange), Ok(percentage) => Ok(percentage), }}
use std::fmt::{self, Display, Formatter};use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::InvalidInput => f.write_str("Invalid input"), Self::OutOfRange => f.write_str("Input out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Ok(p @ 0..=100) => Ok(p), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;// 1. Finish the definition#[derive(Debug)]#[derive(PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Error") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(i) if i <= 100 => Ok(i), Ok(i) if i > 100 => Err(ParsePercentageError::OutOfRange), _ => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use core::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug)]#[derive(PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "WFT") }}impl Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let value = input.parse::<u8>(); match value { Ok(0..=100) => Ok(value.unwrap()), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), } }// Example usagepub fn main() { assert_eq!(parse_percentage("75"), Ok(75)); let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { }impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(value) = input.parse::<u8>() { if 0 <= value && value <= 100 { Ok(value) } else { Err(ParsePercentageError::OutOfRange) } } else { Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError { fn description(&self) -> &str { match self { ParsePercentageError::OutOfRange => "Percentage out of range.", ParsePercentageError::InvalidInput => "Invalid input.", } }}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(p) => { if p > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(p) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use core::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{}", self) }}impl Error for ParsePercentageError { fn description(&self) -> &str { match self { ParsePercentageError::InvalidInput => "Invalid input", ParsePercentageError::OutOfRange => "Percentage out of range", } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<i32>() { Ok(percent) => { if percent < 0 || percent > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(percent as u8) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { fn description(&self) -> &str { match self { ParsePercentageError::InvalidInput => { return "Invalid input." } ParsePercentageError::OutOfRange => { return "Out of range." } } }}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let percentage: Result<u8, _> = input.parse(); println!("parse_percentage: {:?}", percentage); match percentage { Ok(pct) => { if pct > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(pct); } Err(_e) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::InvalidInput => write!(f, "Err(ParsePercentageError::InvalidInput)"), Self::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse() { Ok(n) if n <= 100 => Ok(n) , Ok(_) => Err(ParsePercentageError::OutOfRange), _ => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt::{Debug, Display, Formatter};// 1. Finish the definition#[derive(PartialEq, Debug)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => { write!(f, "Invalid input provided") } ParsePercentageError::OutOfRange => { write!(f, "Out of range") } } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) => { if (0..=100).contains(&num) { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } } Err(_) => { Err(ParsePercentageError::InvalidInput) } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { Self::InvalidInput => write!(f, "{}", "Invalid input"), Self::OutOfRange => write!(f, "{}", "Out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.error_message()) }}impl ParsePercentageError { pub fn error_message(&self) -> &str { match self { Self::InvalidInput => "Invalid input.", Self::OutOfRange => "Out of range.", } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let untrusted_input = input.parse::<u8>(); match untrusted_input { Ok(number) => { if (0..=100).contains(&number) { return Ok(number); } else { return Err(ParsePercentageError::OutOfRange); } } Err(_) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}