Rust Programming for Smart Contract Development
Task Details
In this task, students will add error handling to the basic banking system created in the previous task. The program will handle errors related to depositing and withdrawing money from the bank account.
Steps
- Modify the deposit method of the Account Trait to return a Result<(), String> instead of (). The Result type will be used to handle errors. The String type will be used to hold the error message.
- Modify the withdraw method of the Account Trait to return a Result<(), String> instead of (). The Result type will be used to handle errors. The String type will be used to hold the error message.
- In the implementation of the deposit method for BankAccount, use the Ok(()) variant to indicate success and the Err("Error message".to_string()) variant to indicate failure.
- In the implementation of the withdraw method for BankAccount, use the Ok(()) variant to indicate success and the Err("Error message".to_string()) variant to indicate failure.
- In the main function, create two BankAccount instances with different account numbers and holder names.
- Call the deposit method on one of the accounts, passing in a deposit amount. Handle any errors returned by the deposit method using a match statement.
- Call the withdraw method on the other account, passing in a withdraw amount. Handle any errors returned by the withdraw method using a match statement.
- Call the balance method on both accounts and print the result to the console.
- Compile and run the program to ensure it works as expected.
Checklist
- Modify the deposit method of the Account Trait to return a Result<(), String> instead of ().
- Modify the withdraw method of the Account Trait to return a Result<(), String> instead of ().
- In the implementation of the deposit method for BankAccount, use the Ok(()) variant to indicate success and the Err("Error message".to_string()) variant to indicate failure.
- In the implementation of the withdraw method for BankAccount, use the Ok(()) variant to indicate success and the Err("Error message".to_string()) variant to indicate failure.
- Create two BankAccount instances in the main function with different account numbers and holder names.
- Call the deposit method on one of the accounts, passing in a deposit amount. Handle any errors returned by the deposit method using a match statement.
- Call the withdraw method on the other account, passing in a withdraw amount. Handle any errors returned by the withdraw method using a match statement.
- Call the balance method on both accounts and 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!