Implementation:Avhz RustQuant Money
| Knowledge Sources | |
|---|---|
| Domains | Foreign_Exchange, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Implements the Money struct representing a monetary amount paired with a specific currency, with full arithmetic operator support restricted to same-currency operations.
Description
The Money struct combines a Currency with an f64 amount, forming the fundamental unit for monetary calculations. It derives Debug, Clone, and Copy and provides manual implementations of Eq and PartialEq (comparing by ISO code and amount), PartialOrd (only comparable within the same currency), and Display (showing amount, currency name, and ISO code).
The struct implements all four basic arithmetic operators (Add, Sub, Mul, Div) via the std::ops traits. Each operation enforces currency matching at runtime: if the two operands have different currencies, the operation panics with a descriptive error message. This design prevents accidental cross-currency arithmetic that would be financially meaningless without an exchange rate conversion.
Usage
Use this struct whenever you need to represent and perform arithmetic on monetary values. It is used throughout the FX module as the input and output type for exchange rate conversions.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_instruments/src/fx/money.rs
- Lines: 1-260
Signature
#[derive(Debug, Clone, Copy)]
pub struct Money {
pub currency: Currency,
pub amount: f64,
}
impl Money {
pub fn new(currency: Currency, amount: f64) -> Self;
pub fn currency(&self) -> Currency;
pub fn amount(&self) -> f64;
}
impl std::ops::Add for Money { type Output = Self; fn add(self, other: Self) -> Self; }
impl std::ops::Sub for Money { type Output = Self; fn sub(self, other: Self) -> Self; }
impl std::ops::Mul for Money { type Output = Self; fn mul(self, other: Self) -> Self; }
impl std::ops::Div for Money { type Output = Self; fn div(self, other: Self) -> Self; }
impl Eq for Money {}
impl PartialEq for Money { fn eq(&self, other: &Self) -> bool; }
impl PartialOrd for Money { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering>; }
impl fmt::Display for Money { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result; }
Import
use RustQuant::instruments::fx::money::Money;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| currency | Currency | Yes | The currency denomination of the money |
| amount | f64 | Yes | The numeric monetary amount |
Outputs
| Name | Type | Description |
|---|---|---|
| currency() | Currency | Returns the underlying currency |
| amount() | f64 | Returns the numeric amount |
| Add (+) | Money | Sum of two same-currency Money values; panics if currencies differ |
| Sub (-) | Money | Difference of two same-currency Money values; panics if currencies differ |
| Mul (*) | Money | Product of two same-currency Money values; panics if currencies differ |
| Div (/) | Money | Quotient of two same-currency Money values; panics if currencies differ |
Usage Examples
use RustQuant::instruments::fx::money::Money;
use RustQuant::instruments::fx::currency::{USD, EUR};
// Create money instances
let usd_20 = Money::new(USD, 20.5);
let usd_10 = Money::new(USD, 10.5);
// Arithmetic operations (same currency)
let sum = usd_20 + usd_10; // Money { currency: USD, amount: 31.0 }
let diff = usd_20 - usd_10; // Money { currency: USD, amount: 10.0 }
// Comparison
assert!(usd_20 > usd_10);
// Display
println!("{}", usd_20);
// Output:
// Amount: 20.5
// Name: United States Dollar
// ISO: ISO_4217 { alphabetic: "USD", numeric: "840" }
// Cross-currency operations panic:
// let invalid = Money::new(USD, 10.0) + Money::new(EUR, 5.0); // PANICS