- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 292字
- 2021-07-02 13:35:16
Impl blocks for enums
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.rs
enum PaymentMode {
Debit,
Credit,
Paypal
}
// Bunch of dummy payment handlers
fn 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
}
fn main() {
let payment_mode = get_saved_payment_mode();
payment_mode.pay(512);
}
The preceding code has a method called get_saved_payment_mode(), which returns a user's saved payment mode. This can either be a Credit Card, Debit Card, or Paypal. This is best modeled as an enum, where different payment methods can be added as its variants. The library then provides us with a single pay() method to which we can conveniently provide an amount to pay. This method determines which variant of the enum it is and dispatches methods accordingly to the correct payment service provider, without the library consumer worrying about checking which payment method to use.
Enums are also widely used for modeling state machines, and when combined with match statements, they make state transition code very concise to write. They are also used to model custom error types. When enum variants don't have any data associated with them, they can be used like C enums, where the variants implicitly have integer values starting with 0, but can also be manually tagged with integer (isize) values. This is useful when interacting with foreign C libraries.
- 密碼學原理與Java實現
- 數據庫系統原理及MySQL應用教程
- Learning Linux Binary Analysis
- MySQL數據庫管理與開發(慕課版)
- 網絡爬蟲原理與實踐:基于C#語言
- RSpec Essentials
- C#程序設計(項目教學版)
- Java零基礎實戰
- 編寫高質量代碼:改善Objective-C程序的61個建議
- Docker:容器與容器云(第2版)
- Solr權威指南(下卷)
- Microsoft XNA 4.0 Game Development Cookbook
- Magento 2 Developer's Guide
- Python網絡運維自動化
- Python High Performance(Second Edition)