Chapter 4 - Enum & Patterns
-
Create an enum that contains HTTP 4xx Client Errors.
i.e.
Not Found
Should have value404
associated with it. -
Write an enum to store information of whether a person is a child or adult based on his/her age.
-
Create a calculator with the help of an enum named
Operation
that as values such asAdd, Sub, Div, Multi
.
hint:
For input like2+5*10
it should first evaluate5*10
and then add 2 to it. -
Use pattern matching to associate a enum of
Grade
type with a student based on his/her marks. -
Create an enum named
Json
that can work with arbitrary JSON data. -
Print what kind of input the user has given(numbers, letters, symbols) using pattern matching.
hint:
Try using range for it e.g.0 ... 100
or'a' ... 'k'
-
Use pattern matching to find that whether a point lies on X-Axis, Y-Axis or on which quadrant.
-
Create a struct that holds info about a gun(gun type, recoil time, magazine size, extra) where
gun type
andextra
are enums andextra
contains silencer, scope, extended mags nadNone
.
Based on user input change the value of extra (may cause change in recoil time and magazine size). -
Create a Binary tree and have a method
add
on it to add elements to it andmin
andmax
to find the minimum and maximum element in it. -
Create a
Regex
to extract dates from this stringIt was on 2019-03-14, almost after a year from 2018-02-11
and store in a Struct with fields ofday
,month
andyear
.
hint:
UseRegex
crate