January 2019
Beginner to intermediate
554 pages
13h 31m
English
We can also provide implementations for enums. For example, consider a payments library built in Rust, which exposes a single API called pay:
// enum_methods.rsenum PaymentMode { Debit, Credit, Paypal}// Bunch of dummy payment handlersfn pay_by_credit(amt: u64) { println!("Processing credit payment of {}", amt);}fn pay_by_debit(amt: u64) { println!("Processing debit payment of {}", amt);}fn paypal_redirect(amt: u64) { println!("Redirecting to paypal for amount: {}", amt);}impl PaymentMode { fn pay(&self, amount: u64) { match self { PaymentMode::Debit => pay_by_debit(amount), PaymentMode::Credit => pay_by_credit(amount), PaymentMode::Paypal => paypal_redirect(amount) } }}fn get_saved_payment_mode() -> PaymentMode { PaymentMode::Debit ...Read now
Unlock full access