1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
pub mod currency; #[derive(Debug)] pub struct Money<'a, T> { amount: T, currency: currency::Currency<'a>, } impl<'a> Money<'a, i64> { pub fn new(amount: i64, currency: currency::Currency) -> Money<i64> { Money { amount: amount, currency: currency, } } pub fn format(&self) -> String { let formatted_amount = self.amount as f64 / self.currency.divisor() as f64; return format!( "{code}{symbol}{amount}", code = self.currency.code(), symbol = self.currency.symbol(), amount = formatted_amount ); } }