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.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) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Percentage out of range"), ParsePercentageError::OutOfRange => write!(f, "Invalid input"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) if n < 101 => 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};// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<i32>() { Ok(number) => { if number >= 0 && number <= 100 { Ok(number as u8) } 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::{Display, Formatter};#[derive(Debug, PartialEq)]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"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let value = input .parse() .map_err(|_| ParsePercentageError::InvalidInput)?; if value > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(value)}
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) -> 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 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;// Derive PartialEq for enum comparison#[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, "Input is not a valid number"), 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(num) => { if num <= 100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usage remains the samepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); let result = parse_percentage("101"); println!("{:?}", result); let result = parse_percentage("abc"); println!("{:?}", result);}
use std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}impl fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result // Result<(), Error> { match self { ParsePercentageError::InvalidInput => { write!(f, "InvalidInput") }, ParsePercentageError::OutOfRange => { write!(f, "OutOfRange") } } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let res = input.parse::<u8>(); match res { Ok(v) => { match v { 0..=100 => {Ok(v)}, _ => { 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 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"), } }}impl std::error::Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let parsed = match input.parse::<u8>() { Err(_) => return Err(ParsePercentageError::InvalidInput), Ok(r) => r }; if parsed > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(parsed)}// 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::fmt::{Display, Formatter};#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}impl std::error::Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num:u8 = match input.parse() { Ok(num) => num, Err(_) => return Err(ParsePercentageError::InvalidInput) }; if num<0 || num>100{ return Err(ParsePercentageError::OutOfRange); } 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)}
// 1. Finish the definitionuse std::fmt;use std::fmt::{Display, Formatter};#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}impl std::error::Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num:u8 = match input.parse() { Ok(num) => num, Err(_) => return Err(ParsePercentageError::InvalidInput) }; if num<0 || num>100{ return Err(ParsePercentageError::OutOfRange); } 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)}
// 1. Finish the definitionuse std::fmt;#[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::OutOfRange => write!(f, "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> { // 3. Implement this function 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)}
use std::fmt;// 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, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) if x<=100 => Ok(x), 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` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}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 std::error::Error for ParsePercentageError {}// 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,}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"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) if x<=100 => Ok(x), 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 => "Err(ParsePercentageError::InvalidInput)", ParsePercentageError::OutOfRange => "Err(ParsePercentageError::OutOfRange)" } }}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "An errror occured") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Ok(n) = input.parse::<i32>() { if n > 100 || n < 0 { return Err(ParsePercentageError::OutOfRange); } return Ok(n as u8); } 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)}
// 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 => return write!(f, "Err(ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => return write!(f, "Err(ParsePercentageError::OutOfRange") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) if x <= 100 => Ok(x), 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}// 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 => return write!(f, "Err(ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => return write!(f, "Err(ParsePercentageError::OutOfRange") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) if x <= 100 => Ok(x), 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 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, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => { write!(f, "Percemtage out of range") } ParsePercentageError::OutOfRange => { write!(f, "Invalid Input") } } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), 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;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` trait// 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::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input format"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let number = input.parse::<u8>().map_err(|_| ParsePercentageError::InvalidInput)?; if number > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(number)}// 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::write;use std::cmp::PartialEq;use std::fmt;use std::error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ OutOfRange, InvalidInput}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => return write!(f, "Err(ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => return write!(f, "Err(ParsePercentageError::OutOfRange") } }}impl error::Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Err(_e) => return Err(ParsePercentageError::InvalidInput), Ok(val) => match val { 0..=100 => return Ok(val), _ => 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::fmt;// 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, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) if x<=100 => Ok(x), 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 { match self { ParsePercentageError::InvalidInput => write!(f, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let number = input.trim().parse::<u8>().map_err(|_| ParsePercentageError::InvalidInput)?; if number > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(number)}// 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,}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"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let value = input.parse::<u8>(); match value { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(101..) => Err(ParsePercentageError::OutOfRange), Ok(val) => Ok(val), }}// 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, Formatter, Pointer};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match *self { ParsePercentageError::InvalidInput => write!(f, "InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "OutOfRange"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Ok(percent) => { if percent > 100 { return Err(ParsePercentageError::OutOfRange) } Ok(percent) }, 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)] // Added PartialEq herepub enum ParsePercentageError { InvalidInput, OutOfRange,}// Implement the `Display` trait for ParsePercentageErrorimpl 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 valid percentage range (0-100)."), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}// 3. Implement this functionpub 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)}// Tests (assuming this is the content of tests/tests.rs)#[cfg(test)]mod tests { use super::*; #[test] fn test_valid_percentage() { assert_eq!(parse_percentage("75"), Ok(75)); assert_eq!(parse_percentage("0"), Ok(0)); assert_eq!(parse_percentage("100"), Ok(100)); } #[test] fn test_out_of_range() { assert_eq!( parse_percentage("150"), Err(ParsePercentageError::OutOfRange) ); } #[test] fn test_invalid_input() { assert_eq!( parse_percentage("abc"), Err(ParsePercentageError::InvalidInput) ); }}
#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}use std::fmt;use std::error::Error;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)") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let n = input .parse::<u8>() .map_err(|_0| ParsePercentageError::InvalidInput)?; if n <= 100 { Ok(n) } else { 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, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl 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") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.trim().parse::<u8>() { Ok(percentage) if percentage <= 100 => Ok(percentage), 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 => "Err(ParsePercentageError::InvalidInput)", ParsePercentageError::OutOfRange => "Err(ParsePercentageError::OutOfRange)" } }}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "An errror occured") }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Ok(n) = input.parse::<i32>() { if n > 100 || n < 0 { return Err(ParsePercentageError::OutOfRange); } return Ok(n as u8); } 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, fmt::{Display, Formatter}};#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => { write!(f, "The value is out of range") } ParsePercentageError::InvalidInput => { write!(f, "The input is invalid") } } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { if value > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(value) } }, _ => 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};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", &self) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(val) if val > 100 => Err(ParsePercentageError::OutOfRange), Ok(val) => Ok(val), 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, Eq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::InvalidInput)"), } }}impl std::error::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) } 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::{Display, Formatter};// 1. Finish the definition#[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, "Percentage out of range") } ParsePercentageError::OutOfRange => { write!(f, "Invalid Input") } } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num_input = input.parse::<u8>(); match num_input { 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)}
use std::error::Error;use std::fmt;use std::fmt::Display;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl 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"), } }}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)}
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 { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Value is out of range (0-100)"), } }}impl std::error::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)}
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<'_>) -> fmt::Result { write!(f, "{:?}", self) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<i32>() { Ok(n) if n >=0 && n <= 100 => Ok(n 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::{error::Error, fmt::Display};#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) => { if n <= 100 { Ok(n) } 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<'_>) -> Result<(), std::fmt::Error> { 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 { fn description(&self) -> &str { match self { ParsePercentageError::InvalidInput => "Invalid Input", ParsePercentageError::OutOfRange => "Input Out of Range", } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Err(_) => { return Err(ParsePercentageError::InvalidInput); }, Ok(v) => { if v > 100 { return Err(ParsePercentageError::OutOfRange); } } } Ok(input.parse().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)}
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 std::fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "The input isn't numeric"), ParsePercentageError::OutOfRange => write!(f, "The input is greater than 100"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let res = input.parse::<u8>(); match res { Ok(value) if value <= 100 => Ok(value), 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)}
// 1. Finish the definitionuse std::error::Error;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, "Error: Invalid Input!"), ParsePercentageError::OutOfRange => write!(f, "Error: Out Of Range!"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(number) => { if number <= 100 { Ok(number) } 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, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Error percentage") }}impl Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) => { if value <= 100 { Ok(value) } 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, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Error percentage") }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(number) => { if number <= 100 { Ok(number) } 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 { write!(f, "Pasrse Error!") }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(value) = input.parse::<u8>() { if 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)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "parse percentage error") }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Ok(value) = str::parse::<u8>(input) { return if value <= 100 { Ok(value) } else { Err(ParsePercentageError::OutOfRange) }; } 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::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, Eq, 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 { write!(f, "{:?}", self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(a) if a <= 100 => Ok(a), 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)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { 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> { // 3. Implement this function let n = input.parse::<u8>().map_err(|_| ParsePercentageError::InvalidInput)?; if n > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(n)}// 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::fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f,"ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => write!(f,"ParsePercentageError::OutOfRange"), } }}impl std::error::Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError>{ match input.parse::<u8>() { Ok(n) if n <=100 => Ok(n), Err(_) => Err(ParsePercentageError::InvalidInput), _ => 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 => { return write!(f, "invalid input"); }, ParsePercentageError::OutOfRange => { return write!(f, "out of range item"); } } }}impl Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(i) = input.parse::<u8>() { if i > 100 || i < 0 { return Err(ParsePercentageError::OutOfRange); } return Ok(i); } 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::{Debug, Display, Formatter};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => { write!(f, "The value is out of range") } ParsePercentageError::InvalidInput => { write!(f, "The input is invalid") } } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let x = input .parse::<i64>() .map_err(|_| ParsePercentageError::InvalidInput)?; if x < 0 || x > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(x 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)}
// 1. Finish the definitionuse std::error::Error;use std::fmt;#[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 { Self::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), Self::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Ok(val) = input.parse::<u8>() { if val > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(val); } 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::Display;const ERR_INVALID_INPUT: &str = "Invalid input";const ERR_PERCENTAGE_OUT_OF_RANGE: &str = "Percentage out of range";#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { use ParsePercentageError::*; let err_str = match self { InvalidInput => ERR_INVALID_INPUT, OutOfRange => ERR_PERCENTAGE_OUT_OF_RANGE, } .to_string(); write!(f, "{err_str}") }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let p = input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput)?; if p > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(p) }}// 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{ Self::InvalidInput => write!(f, "This is my personalized Invalid Input"), Self::OutOfRange => write!(f, "This is my personal Out Of Range") } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{ //trait Error: Display + Debug} pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ Ok(number)=>{ if number<=100{ Ok(number) }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)}