Currency

Struct Currency 

Source
pub struct Currency {
    pub symbol: Option<char>,
    pub value: i64,
}
Expand description

Represents currency through an optional symbol and amount of coin.

Each 100 coins results in a banknote. (100 is formatted as 1.00) The currency will be formatted as such: Currency(Some('$'), 432) ==> โ€œ$4.32โ€

Fieldsยง

ยงsymbol: Option<char>

Currency symbol

pick any of โ‚ฌ, ยฃ, $, ยฅ etcโ€ฆ

ยงvalue: i64

value in the smallest possible unit

Implementationsยง

Sourceยง

impl Currency

Source

pub fn new() -> Currency

Creates a blank Currency as Currency(None, 0)

ยงExamples
let mut c = Currency::new();
Source

pub fn from_value(value: i64) -> Currency

Initialize from i64

Source

pub fn postfix(&self) -> Postfix<'_>

Returns an object that implements Display for different methods of printing currency.

Examples found in repository?
examples/catalogue.rs (line 18)
11fn print_items(items: &[BillItem<Product<'_>>]) {
12    //println!("{:?}", items);
13    for item in items {
14        println!(
15            "   * {:3}x {:15} {:6} {:6}",
16            item.amount,
17            item.product.name,
18            item.product.price.postfix(),
19            item.gross().postfix()
20        );
21    }
22}
23
24fn print(title: &str, bill: &Bill<Product<'_>>) {
25    println!("{}:", title);
26    for (tax, items) in &bill.items_by_tax {
27        println!("  {}%", tax);
28        print_items(items);
29    }
30    println!("---------------------------------------");
31    println!("       {}", bill.gross_total().postfix());
32    println!("     + {} (tax)", bill.tax_total().postfix());
33    println!("   net {}", bill.net_total().postfix());
34    println!();
35}
Source

pub fn prefix(&self) -> Prefix<'_>

Returns an object that implements Display for different methods of printing currency.

Source

pub fn as_float(&self) -> f64

Returns the value as float

ยงWarning, do not use this for calculation, this is for displaying only!
Source

pub fn value(&self) -> i64

Returns the inner value

Source

pub fn symbol(&self) -> Option<char>

Returns the inner symbol

Methods from Deref<Target = i64>ยง

1.43.0 ยท Source

pub const MIN: i64 = -9_223_372_036_854_775_808i64

1.43.0 ยท Source

pub const MAX: i64 = 9_223_372_036_854_775_807i64

1.53.0 ยท Source

pub const BITS: u32 = 64u32

Trait Implementationsยง

Sourceยง

impl Add for Currency

Overloads the โ€˜+โ€™ operator for Currency objects.

ยงPanics

Panics if the two addends are different types of currency, as denoted by the Currencyโ€™s symbol.

Sourceยง

type Output = Currency

The resulting type after applying the + operator.
Sourceยง

fn add(self, rhs: Currency) -> Currency

Performs the + operation. Read more
Sourceยง

impl Clone for Currency

Sourceยง

fn clone(&self) -> Currency

Returns a duplicate 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 Currency

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl Default for Currency

Sourceยง

fn default() -> Currency

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl Deref for Currency

Required for DerefMut

Sourceยง

type Target = i64

The resulting type after dereferencing.
Sourceยง

fn deref(&self) -> &i64

Dereferences the value.
Sourceยง

impl Div<i64> for Currency

Overloads the โ€˜/โ€™ operator for Currency objects.

Allows a Currency to be divided by an i64.

Sourceยง

type Output = Currency

The resulting type after applying the / operator.
Sourceยง

fn div(self, rhs: i64) -> Currency

Performs the / operation. Read more
Sourceยง

impl From<(char, i64)> for Currency

Sourceยง

fn from(tpl: (char, i64)) -> Currency

converts from a tuple of symbol and i64

Sourceยง

impl From<(i64, char)> for Currency

Sourceยง

fn from(tpl: (i64, char)) -> Currency

converts from a tuple of i64 and symbol

Sourceยง

impl From<i64> for Currency

Sourceยง

fn from(value: i64) -> Currency

converts from a i64

Sourceยง

impl Mul<Currency> for i64

Overloads the โ€˜*โ€™ operator for i64.

Allows an i64 to be multiplied by a Currency. Completes the commutative property for i64 multiplied by Currency.

Sourceยง

type Output = Currency

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: Currency) -> Currency

Performs the * operation. Read more
Sourceยง

impl Mul<f64> for Currency

Multiplies with float, probably not a good idea, help appreciated.

Sourceยง

type Output = Currency

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: f64) -> Currency

Performs the * operation. Read more
Sourceยง

impl Mul<i64> for Currency

Overloads the โ€˜*โ€™ operator for Currency objects.

Allows a Currency to be multiplied by an i64.

Sourceยง

type Output = Currency

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: i64) -> Currency

Performs the * operation. Read more
Sourceยง

impl PartialEq for Currency

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl PartialOrd for Currency

Sourceยง

fn partial_cmp(&self, other: &Currency) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Sourceยง

impl Serialize for Currency

Sourceยง

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Sub for Currency

Overloads the โ€˜-โ€™ operator for Currency objects.

ยงPanics

Panics if the minuend and subtrahend are two different types of currency, as denoted by the Currencyโ€™s symbol.

Sourceยง

type Output = Currency

The resulting type after applying the - operator.
Sourceยง

fn sub(self, rhs: Currency) -> Currency

Performs the - operation. Read more
Sourceยง

impl Copy for Currency

Sourceยง

impl Eq for Currency

Sourceยง

impl StructuralPartialEq for Currency

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

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

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

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

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

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

Sourceยง

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

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Sourceยง

type Target = T

๐Ÿ”ฌThis is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

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, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

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 T
where U: TryFrom<T>,

Sourceยง

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.