Currency

Struct Currency 

Source
pub struct Currency {
    pub symbol: Option<Symbol>,
    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<Symbol>

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/initialization.rs (line 6)
3fn display(my_money: Currency) {
4    println!("I have {:?}!", my_money);
5    //println!("I have {}!", my_money); // doesn't compile :D
6    println!("I have {}!", my_money.postfix()); // -> "I have 47,11€!"
7    println!("I have {}!", my_money.prefix()); // -> "I have €47.11!"
8    println!()
9}
More examples
Hide additional examples
examples/display.rs (line 11)
3fn main() {
4    let my_money = Currency {
5        symbol: Some('€'),
6        value: 4711,
7    };
8
9    println!("I have {:?}!", my_money);
10    //println!("I have {}!", my_money); // doesn't compile :D
11    println!("I have {}!", my_money.postfix()); // -> "I have 47,11€!"
12    println!("I have {}!", my_money.prefix()); // -> "I have €47.11!"
13}
Source

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

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

Examples found in repository?
examples/initialization.rs (line 7)
3fn display(my_money: Currency) {
4    println!("I have {:?}!", my_money);
5    //println!("I have {}!", my_money); // doesn't compile :D
6    println!("I have {}!", my_money.postfix()); // -> "I have 47,11€!"
7    println!("I have {}!", my_money.prefix()); // -> "I have €47.11!"
8    println!()
9}
More examples
Hide additional examples
examples/display.rs (line 12)
3fn main() {
4    let my_money = Currency {
5        symbol: Some('€'),
6        value: 4711,
7    };
8
9    println!("I have {:?}!", my_money);
10    //println!("I have {}!", my_money); // doesn't compile :D
11    println!("I have {}!", my_money.postfix()); // -> "I have 47,11€!"
12    println!("I have {}!", my_money.prefix()); // -> "I have €47.11!"
13}
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<Symbol>

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

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

impl Default for Currency

Source§

fn default() -> Self

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: (Symbol, i64)) -> Currency

converts from a tuple of symbol and i64

Source§

impl From<(i64, char)> for Currency

Source§

fn from(tpl: (i64, Symbol)) -> 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 f64

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<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 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.