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(PartialEq, Debug)]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, "anhdoo") } ParsePercentageError::OutOfRange => { write!(f, "anhdoooo") } } }}impl Error for ParsePercentageError{ fn source(&self) -> Option<&(dyn Error + 'static)>{ None }}pub 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(_e) => { 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 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 {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) => { if n > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(n) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
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)}
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 { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input for percentage parsing"), ParsePercentageError::OutOfRange => write!(f, "Percentage value out of allowed range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { 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::error::Error;use std::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<'_>) -> Result<(), std::fmt::Error> { 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.parse::<u8>() { Ok(n) => { if n > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(n) } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::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<'_>) -> Result<(), std::fmt::Error> { 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.parse::<u8>() { Ok(n) => { if n > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(n) } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::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<'_>) -> Result<(), std::fmt::Error> { 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.parse::<u8>() { Ok(n) => { if n > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(n) } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::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<'_>) -> Result<(), std::fmt::Error> { 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.parse::<u8>() { Ok(n) => { if n > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(n) } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::error::Error;use std::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<'_>) -> Result<(), std::fmt::Error> { match self { Self::InvalidInput => write!(f, "Invalid input"), Self::OutOfRange => write!(f, "Out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n @ 0..=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)}
// 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<'_>) -> Result<(), std::fmt::Error> { match self { Self::InvalidInput => write!(f, "Invalid input"), Self::OutOfRange => write!(f, "Out of range"), } }}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), }}// 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, "The input was invalid. Please use a number"), ParsePercentageError::OutOfRange => write!(f, "The input was out of range. Please use a number between 0 and 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) } 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;// 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 { f.write_fmt(format_args!("The error is {}", self)) }}// 2. Implement the `Error` traitimpl 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) } 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;// 1. Finish the definition#[derive(PartialEq, Debug)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}impl Display for ParsePercentageError { // add code here fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!("The error is {}", self)) }}impl Error for ParsePercentageError { // add code here}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|value| { if value <= 100 { Ok(value) } 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::fmt::Formatter;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 Formatter<'_>) -> 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(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)}
// 1. Finish the definitionuse std::fmt::Debug;#[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, "ParsePercentageError::OutOfRange"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::InvalidInput"), } }}impl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { 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)}
// 1. Finish the definitionuse std::fmt::Debug;#[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, "ParsePercentageError::OutOfRange"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::InvalidInput"), } }}impl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { 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::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "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 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 {}// 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, "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.parse() { Ok(num) => { if num > 100 || num < 0 { return Err(ParsePercentageError::OutOfRange); } else { return Ok(num); } }, 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;// 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, "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(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)}
use std::error::Error;use std::str::FromStr;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<'_>) -> Result<(), std::fmt::Error> { write!(f, "{:?}", self) }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match u8::from_str(input) { 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::fmt;use std::error::Error;// 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 { match &self { ParsePercentageError::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::OutOfRange") } }}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) => match x { n if n <= 100 => Ok(n), _ => 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;use std::str::FromStr;// 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, "ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::OutOfRange") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let res = u8::from_str(input); match res { Ok(n) => { if n > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(n) }, 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, fmt};#[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 { match self { ParsePercentageError::InvalidInput => write!(f, "invalid input"), ParsePercentageError::OutOfRange => write!(f, "out of range") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(val) if val <= 100 => Ok(val), 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 definitionuse std::error::Error;use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl 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, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Number is out of range (0-100"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(percentage) => { if percentage <= 100 { Ok(percentage) } 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 definitionuse std::error::Error;use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl 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, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Number is out of range (0-100"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(percentage) => { if percentage >= 0 && percentage <= 100 { Ok(percentage) } 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 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 range (0-100"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.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)}
// 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 range (0-100"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.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::Formatter;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input format"), ParsePercentageError::OutOfRange => write!(f, "Value is out of range (0-100)"), } }}// 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 num<=100 { Ok(num) }else{ Err(ParsePercentageError::OutOfRange) } }, Err(_e) => 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 { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Number is out of range (0-100)"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.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)}
// 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 { Self::InvalidInput => write!(f, "Cannot be parsed as Number."), Self::OutOfRange => write!(f, "Number is out of range..") } } }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 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<'_>) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Input is not a valid number."), Self::OutOfRange => write!(f, "Number is out of the valid range.") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) => { if n>100{ Err(ParsePercentageError::OutOfRange) }else{ Ok(n) } } // 输出: 转换后的数字是: 123 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{}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Input is not a valid number."), Self::OutOfRange => write!(f, "Number is out of the valid range.") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::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 { todo!() }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let number = input.parse::<u8>(); match number { Ok(number) => { if number <= 100 { Ok(number) } else { 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 fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Invalid input"), Self::OutOfRange => write!(f, "Percentage out of range") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Ok(num) = input.parse::<i32>() { if num < 0 || num > 100 { return Err(ParsePercentageError::OutOfRange); } else { return Ok(num 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::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 { Self::InvalidInput => write!(f, "Input is not a valid number."), Self::OutOfRange => write!(f, "Number is out of the valid range.") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let num = input.parse::<u8>(); match num { Ok(n) => {if n <= 100 {return Ok(n);}}, Err(_)=> return 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::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 { Self::InvalidInput => write!(f, "Input is not a valid number."), Self::OutOfRange => write!(f, "Number is out of the valid range.") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 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, "Err(ParsePercentageError::InvalidInput)") } ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}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 { return Err(ParsePercentageError::OutOfRange); }; Ok(value) } 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 valid range (0-100)") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::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, "Value is out of range") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let solution = input.parse(); match solution { Ok(val) => { if val <= 100 { Ok(val) } 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, 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 std::fmt::Formatter<'_>) -> fmt::Result { match self { Self::InvalidInput => { write!(f, "Percentage out of range") } Self::OutOfRange => { write!(f, "Invalid input") } } }}impl error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|value| { if value > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(value) } })}// 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;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { match value <= 100 { true => Ok(value), false => 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;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { match value <= 100 { true => Ok(value), false => 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;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { match value <= 100 { true => Ok(value), false => 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, 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 std::fmt::Formatter<'_>) -> std::fmt::Result { match *self { ParsePercentageError::InvalidInput => write!(f, "The input is not a valid number."), ParsePercentageError::OutOfRange => write!(f, "The input is out of the valid percentage range (0-100)."), } } }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) => { match value <= 100 { true => Ok(value), false => 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::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, "The input is not a valid number."), ParsePercentageError::OutOfRange => write!(f, "The input is out of the valid percentage range (0-100)."), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(x) if x > 100 => Err(ParsePercentageError::OutOfRange), Ok(x) => Ok(x), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match *self { ParsePercentageError::InvalidInput => write!(f, "The input is not a valid number."), ParsePercentageError::OutOfRange => write!(f, "The input is out of the valid percentage range (0-100)."), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(x) if x > 100 => Err(ParsePercentageError::OutOfRange), Ok(x) => Ok(x), 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 definitionuse 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, "{}", format!("{:?}", self)) }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { input .parse::<u8>() .map_err(|_|ParsePercentageError::InvalidInput) .and_then(|result| { if let 0..=100 = result { Ok(result) } 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 { OutOfRange, InvalidInput,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let percent = input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput); if let Ok(v) = percent { if v > 100 { return Err(ParsePercentageError::OutOfRange); } } percent}// 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 { match self { Self::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), Self::OutOfRange => write!(f, "ParsePercentageError::OutOfRange") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function input.parse::<u8>().map_err(|_e| ParsePercentageError::InvalidInput).and_then(|n| 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)}
// 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, "Err(ParsePercentageError::InvalidInput)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let parsed = match input.parse::<u8>() { Ok(v) => v, Err(_) => return Err(ParsePercentageError::InvalidInput) }; 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)}