pub struct ExchangeRate {
pub from_currency: Currency,
pub to_currency: Currency,
pub rate: f64,
}
Expand description
ExchangeRate
struct to hold exchange rate information.
Fields§
§from_currency: Currency
From currency
to_currency: Currency
To currency
rate: f64
The actual exchange rate as a ratio from_currency/to_currency
Implementations§
Source§impl ExchangeRate
impl ExchangeRate
Sourcepub fn new(
from_currency: Currency,
to_currency: Currency,
rate: f64,
) -> ExchangeRate
pub fn new( from_currency: Currency, to_currency: Currency, rate: f64, ) -> ExchangeRate
Create a new exchange rate.
Sourcepub fn convert(&self, money: Money) -> Money
pub fn convert(&self, money: Money) -> Money
Convert money from one currency to another using this exchange rate.
It panics if the money’s currency doesn’t match with from_currency
.
§Example
use RustQuant::instruments::*;
use RustQuant::utils::assert_approx_equal;
// Use USD and EUR currency constants from the money module.
let usd = Money::new(USD, 100.0);
let eur_usd = ExchangeRate::new(USD, EUR, 0.9186955); // 1 USD = 0.9186955 EUR
let eur = eur_usd.convert(usd);
assert_approx_equal!(eur.amount, 91.86955, 1e-5);
assert_eq!(eur.currency, EUR);
It panics if the money’s currency doesn’t match with from_currency
.
ⓘ
use RustQuant::instruments::*;
let usd = Money::new(EUR, 100.0); // Notice the wrong currency
let eur_usd = ExchangeRate::new(USD, EUR, 0.9186955); // 1 USD = 0.9186955 EUR
eur_usd.convert(usd); // This will panic
Trait Implementations§
Source§impl Clone for ExchangeRate
impl Clone for ExchangeRate
Source§fn clone(&self) -> ExchangeRate
fn clone(&self) -> ExchangeRate
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for ExchangeRate
impl Debug for ExchangeRate
impl Copy for ExchangeRate
Auto Trait Implementations§
impl Freeze for ExchangeRate
impl RefUnwindSafe for ExchangeRate
impl Send for ExchangeRate
impl Sync for ExchangeRate
impl Unpin for ExchangeRate
impl UnwindSafe for ExchangeRate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.