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 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)}
use std::{error::Error, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Value out of range for percentage"), ParsePercentageError::InvalidInput => write!(f, "Invalid input for percentage"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if input.chars().any(|it| !it.is_ascii_digit()) { return Err(ParsePercentageError::InvalidInput); } match input.parse::<i32>() { Ok(x) => { if !(0..=100).contains(&x) { Err(ParsePercentageError::OutOfRange) } else { Ok(x 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::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError { fn description(&self) -> &str { match self { ParsePercentageError::InvalidInput => "Invalid input", ParsePercentageError::OutOfRange => "Out of range", } }}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { write!(f, "{}", self.to_string()) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<i32>() { Ok(n) => { if n < 0 || n > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(n 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::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 { write!(f, "") }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let percentage = match input.parse::<u8>() { Ok(percentage) => percentage, Err(_) => return Err(ParsePercentageError::InvalidInput), }; if percentage > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(percentage)}// 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 std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Not a valid number!"), ParsePercentageError::OutOfRange => write!(f, "Must be a number between 0-100!") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(result) if (0..=100).contains(&result) => Ok(result), 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::Display;// 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 std::fmt::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 percentage:u8 = input.parse().map_err(|_| ParsePercentageError::InvalidInput)?; if percentage > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(percentage)}// 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 { ParsePercentageError::InvalidInput => { write!(f, "Input is not a valid number.") } ParsePercentageError::OutOfRange => { write!(f, "Number is out of the valid range (0-100).") } } }}pub 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(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 std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Not a valid number!"), ParsePercentageError::OutOfRange => write!(f, "Must be a number between 0-100!") } }}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(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<'_>) -> fmt::Result { write!(f, "ParsePercentageError") }}impl Error for ParsePercentageError { fn source(&self) -> Option<&(dyn Error + 'static)> { Some(self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(i) => { if (0..=100).contains(&i) { Ok(i) } 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::{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 { let msg = match self { ParsePercentageError::InvalidInput => "Invalid input", ParsePercentageError::OutOfRange => "Out of range" }; write!(f, "{}", msg) }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num: u8 = match input.parse() { Ok(t) => t, Err(_) => return Err(ParsePercentageError::InvalidInput) }; if num > 100 { Err(ParsePercentageError::OutOfRange) } else { 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::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<'_>) -> Result<(), std::fmt::Error> { match self { ParsePercentageError::InvalidInput => writeln!(f, "InvalidInput Error"), ParsePercentageError::OutOfRange => writeln!(f, "OutOfRange Error"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if let Some(percentage) = input.parse::<u8>().ok() { if percentage > 100 { return Err(ParsePercentageError::OutOfRange); } else { return Ok(percentage); } } 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)}
// 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 { 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 if let Ok(p) = input.parse::<u8>() { if p <= 100 { Ok(p) } 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};#[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, "Err(ParsePercentageError::InvalidInput)") } ParsePercentageError::OutOfRange => { write!(f, "Err(ParsePercentageError::OutOfRange)") } } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(p) if (0..=100).contains(&p) => Ok(p), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => 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<> { match self { Self::InvalidInput => write!(f, "invalid input"), Self::OutOfRange => write!(f, "out of range"), } } }impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<i32, ParsePercentageError> { let num = input.parse::<i32>().map_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)}
use std::fmt::{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<'_>) -> std::fmt::Result { f.write_fmt(format_args!("{:?}", self)) }}impl Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // TODO: Implement the function here let percentage = input.parse::<u8>().map_err(|e| ParsePercentageError::InvalidInput)?; if percentage > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(percentage) }}// 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)}