How it works...

Aliases work in a fairly simple manner. Its main role is to ease the usage of a particular type and maintain different types across different modules.

In this recipe, we used the type keyword to create an alias for Result<T, ParseIntError> as AliasedResult<T>; we used this as the type of all the units of the code.

Let's go through the different functional units of the code:

  • double_number: This function takes in the str input and returns AliasedResult<T>. It parses the string to a value, and if it is a valid integer, we use the map function to multiply the input value by 2; else, we have the Err case.
  • print: This function takes in AliasedResult<T>, and using the match statement, we check whether we have an Ok or Err case for printing the corresponding statement.

In the main function, we called print(double_number("10")), which printed the Ok case statements due to valid input, but print(double_number("t")) pushed back an error due to invalid input.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.200.14