Struct money2::Money

source ·
pub struct Money {
    pub amount: Decimal,
    pub currency: Currency,
}
Expand description

An amount of Currency.

To find out how much the amount would be in another Currency, use exchange.

See also

Fields§

§amount: Decimal

The amount of Currency that this Money represents.

§currency: Currency

The Currency that this Money is in.

Implementations§

source§

impl Money

source

pub fn checked_add(self, rhs: Self) -> Option<Self>

Returns Some if rhs is the same Currency and doesn’t over/underflow.

Examples
use money2::{Currency, Money};

let a = Money::new(20_00, 2, Currency::Usd);

assert_eq!(a.checked_add(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
  a.checked_add(Money::new(5, 0, Currency::Usd)),
  Some(Money::new(25, 0, Currency::Usd))
);
source

pub fn checked_div(self, rhs: Self) -> Option<Self>

Returns Some if rhs is the same Currency and doesn’t over/underflow.

Examples
use money2::{Currency, Money};

let a = Money::new(20_00, 2, Currency::Usd);

assert_eq!(a.checked_div(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
  a.checked_div(Money::new(2, 0, Currency::Usd)),
  Some(Money::new(10, 0, Currency::Usd))
);
source

pub fn checked_mul(self, rhs: Self) -> Option<Self>

Returns Some if rhs is the same Currency and doesn’t over/underflow.

Examples
use money2::{Currency, Money};

let a = Money::new(20_00, 2, Currency::Usd);

assert_eq!(a.checked_mul(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
  a.checked_mul(Money::new(2, 0, Currency::Usd)),
  Some(Money::new(40, 0, Currency::Usd))
);
source

pub fn checked_rem(self, rhs: Self) -> Option<Self>

Returns Some if rhs is the same Currency and doesn’t over/underflow.

Examples
use money2::{Currency, Money};

let a = Money::new(20_00, 2, Currency::Usd);

assert_eq!(a.checked_rem(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
  a.checked_rem(Money::new(3, 0, Currency::Usd)),
  Some(Money::new(2, 0, Currency::Usd))
);
source

pub fn checked_sub(self, rhs: Self) -> Option<Self>

Returns Some if rhs is the same Currency and doesn’t over/underflow.

Examples
use money2::{Currency, Money};

let a = Money::new(20_00, 2, Currency::Usd);

assert_eq!(a.checked_sub(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
  a.checked_sub(Money::new(5, 0, Currency::Usd)),
  Some(Money::new(15, 0, Currency::Usd))
);
source

pub fn new(amount: i64, decimal_places: u32, currency: Currency) -> Self

Create new Money.

Examples
use money2::{Currency, Decimal, Money};

let literal = "20.00 USD";
let money = Money::new(20_00, 2, Currency::Usd);

assert_eq!(Money::try_from(literal).unwrap(), money);
assert_eq!(money.to_string(), literal);

Trait Implementations§

source§

impl Add for Money

source§

fn add(self, rhs: Self) -> Self::Output

Panics
  • If this currency and the rhs’s currency are not the same.
  • When [Decimal::add] does.
See also
Examples
use money2::{Currency, Money};

assert_eq!(
  Money::new(10, 0, Currency::Eur) + Money::new(0_50, 2, Currency::Eur),
  Money::new(10_50, 2, Currency::Eur)
);
let _ = Money::new(10, 0, Currency::Eur) + Money::new(0_50, 2, Currency::Usd);
§

type Output = Money

The resulting type after applying the + operator.
source§

impl AddAssign for Money

source§

fn add_assign(&mut self, rhs: Self)

Panics
See also
Examples
use money2::{Currency, Money};

let mut foo = Money::new(10, 0, Currency::Eur);
foo += Money::new(0_50, 2, Currency::Eur);
assert_eq!(foo, Money::new(10_50, 2, Currency::Eur));
foo += Money::new(0_50, 2, Currency::Usd);
source§

impl Clone for Money

source§

fn clone(&self) -> Money

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Money

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Money

source§

fn default() -> Money

Returns the “default value” for a type. Read more
source§

impl Display for Money

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Div for Money

source§

fn div(self, rhs: Self) -> Self::Output

Panics
  • If this currency and the operand’s currency are not the same.
  • When [Decimal::div] does.
See also
Examples
use money2::{Currency, Money};

assert_eq!(
  Money::new(10, 0, Currency::Eur) / Money::new(2, 0, Currency::Eur),
  Money::new(5, 0, Currency::Eur)
);
let _ = Money::new(10, 0, Currency::Eur) / Money::new(2, 0, Currency::Usd);
§

type Output = Money

The resulting type after applying the / operator.
source§

impl DivAssign for Money

source§

fn div_assign(&mut self, rhs: Self)

Panics
See also
Examples
use money2::{Currency, Money};

let mut foo = Money::new(10, 0, Currency::Eur);
foo /= Money::new(2, 0, Currency::Eur);
assert_eq!(foo, Money::new(5, 0, Currency::Eur));
foo /= Money::new(2, 0, Currency::Usd);
source§

impl Exchange for Money

source§

fn exchange_mut(&mut self, currency: Currency, rates: &ExchangeRates)

The result will be rounded to two decimal places.

See also
source§

fn exchange(self, currency: Currency, rates: &ExchangeRates) -> Selfwhere Self: Sized,

Exchange some quantity into another currency using rates. Derived from the exchange_mut implementation.
source§

impl FromStr for Money

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Money

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Mul for Money

source§

fn mul(self, rhs: Self) -> Self::Output

Panics
  • If this currency and the operand’s currency are not the same.
  • When [Decimal::mul] does.
See also
Examples
use money2::{Currency, Money};

assert_eq!(
  Money::new(10, 0, Currency::Eur) * Money::new(2, 0, Currency::Eur),
  Money::new(20, 0, Currency::Eur)
);
let _ = Money::new(10, 0, Currency::Eur) * Money::new(2, 0, Currency::Usd);
§

type Output = Money

The resulting type after applying the * operator.
source§

impl MulAssign for Money

source§

fn mul_assign(&mut self, rhs: Self)

Panics
See also
Examples
use money2::{Currency, Money};

let mut foo = Money::new(10, 0, Currency::Eur);
foo *= Money::new(2, 0, Currency::Eur);
assert_eq!(foo, Money::new(20, 0, Currency::Eur));
foo *= Money::new(2, 0, Currency::Usd);
source§

impl Ord for Money

source§

fn cmp(&self, other: &Money) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Money

source§

fn eq(&self, other: &Money) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Money

source§

fn partial_cmp(&self, other: &Money) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Rem for Money

source§

fn rem(self, rhs: Self) -> Self::Output

Panics
  • If this currency and the operand’s currency are not the same.
  • When [Decimal::rem] does.
See also
Examples
use money2::{Currency, Money};

assert_eq!(
  Money::new(10, 0, Currency::Eur) % Money::new(3, 0, Currency::Eur),
  Money::new(1, 0, Currency::Eur)
);
let _ = Money::new(10, 0, Currency::Eur) % Money::new(3, 0, Currency::Usd);
§

type Output = Money

The resulting type after applying the % operator.
source§

impl RemAssign for Money

source§

fn rem_assign(&mut self, rhs: Self)

Panics
See also
Examples
use money2::{Currency, Money};

let mut foo = Money::new(10, 0, Currency::Eur);
foo %= Money::new(3, 0, Currency::Eur);
assert_eq!(foo, Money::new(1, 0, Currency::Eur));
foo %= Money::new(3, 0, Currency::Usd);
source§

impl Sub for Money

source§

fn sub(self, rhs: Self) -> Self::Output

Panics
  • If this currency and the operand’s currency are not the same.
  • When [Decimal::sub] does.
See also
Examples
use money2::{Currency, Money};

assert_eq!(
  Money::new(10, 0, Currency::Eur) - Money::new(2, 0, Currency::Eur),
  Money::new(8, 0, Currency::Eur)
);
let _ = Money::new(10, 0, Currency::Eur) - Money::new(2, 0, Currency::Usd);
§

type Output = Money

The resulting type after applying the - operator.
source§

impl SubAssign for Money

source§

fn sub_assign(&mut self, rhs: Self)

Panics
See also
Examples
use money2::{Currency, Money};

let mut foo = Money::new(10, 0, Currency::Eur);
foo -= Money::new(2, 0, Currency::Eur);
assert_eq!(foo, Money::new(8, 0, Currency::Eur));
foo -= Money::new(2, 0, Currency::Usd);
source§

impl TryFrom<&str> for Money

§

type Error = <Money as FromStr>::Err

The type returned in the event of a conversion error.
source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for Money

source§

impl Eq for Money

source§

impl StructuralEq for Money

source§

impl StructuralPartialEq for Money

Auto Trait Implementations§

§

impl RefUnwindSafe for Money

§

impl Send for Money

§

impl Sync for Money

§

impl Unpin for Money

§

impl UnwindSafe for Money

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Comparable<K> for Qwhere Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T, Rhs> NumAssignOps<Rhs> for Twhere T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for Twhere T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,