Learn everything about Rust
Task Details
In this task, students will create a simple calculator in Rust that can perform basic arithmetic operations using enums and pattern matching.
Steps
- Create an enum called Operation with variants Add, Subtract, Multiply, and Divide. Each variant should hold two f64 values.
- Create a function called calculate that takes an Operation enum as an argument and returns an f64 result.
- Implement the calculate function using pattern matching to perform the appropriate arithmetic operation based on the variant of the Operation enum.
- In the main function, prompt the user to input the first number, the operation to be performed, and the second number.
- Parse the user input into appropriate variables.
- Create an Operation enum instance with the parsed input values.
- Call the calculate function with the created Operation enum instance.
- Print the result to the console.
- Compile and run the program to ensure it works as expected.
Checklist
- Define the Operation enum with the appropriate variants and values.
- Write the calculate function signature.
- Implement the calculate function using pattern matching to perform the appropriate arithmetic operation.
- Prompt the user to input the first number, operation, and second number.
- Parse the user input into appropriate variables.
- Create an Operation enum instance with the parsed input values.
- Call the calculate function with the created Operation enum instance.
- Print the result to the console.
- Compile and run the program to test its functionality.
Project
Comments
You need to enroll in the course to be able to comment!