Expand description
A wrapper for a number, which can be either f64
or i64
.
Variants
Integer(i64)
Float(Float)
Implementations
sourceimpl Number
impl Number
sourcepub fn into_f64(self) -> f64
pub fn into_f64(self) -> f64
Returns the f64
representation of the number regardless of whether the number is stored
as a float or integer.
Example
let i = Number::new(5);
let f = Number::new(2.0);
assert_eq!(i.into_f64(), 5.0);
assert_eq!(f.into_f64(), 2.0);
sourcepub fn as_f64(self) -> Option<f64>
pub fn as_f64(self) -> Option<f64>
If the Number
is a float, return it. Otherwise return None
.
Example
let i = Number::new(5);
let f = Number::new(2.0);
assert_eq!(i.as_f64(), None);
assert_eq!(f.as_f64(), Some(2.0));
sourcepub fn as_i64(self) -> Option<i64>
pub fn as_i64(self) -> Option<i64>
If the Number
is an integer, return it. Otherwise return None
.
Example
let i = Number::new(5);
let f = Number::new(2.0);
assert_eq!(i.as_i64(), Some(5));
assert_eq!(f.as_i64(), None);
sourcepub fn map_to<T>(
self,
integer_fn: impl FnOnce(i64) -> T,
float_fn: impl FnOnce(f64) -> T
) -> T
pub fn map_to<T>(
self,
integer_fn: impl FnOnce(i64) -> T,
float_fn: impl FnOnce(f64) -> T
) -> T
Map this number to a single type using the appropriate closure.
Example
let i = Number::new(5);
let f = Number::new(2.0);
assert!(i.map_to(|i| i > 3, |f| f > 3.0));
assert!(!f.map_to(|i| i > 3, |f| f > 3.0));
Trait Implementations
sourceimpl Ord for Number
impl Ord for Number
sourceimpl PartialOrd<Number> for Number
impl PartialOrd<Number> for Number
sourcefn partial_cmp(&self, other: &Number) -> Option<Ordering>
fn partial_cmp(&self, other: &Number) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
impl Copy for Number
impl Eq for Number
impl StructuralEq for Number
impl StructuralPartialEq for Number
Auto Trait Implementations
impl RefUnwindSafe for Number
impl Send for Number
impl Sync for Number
impl Unpin for Number
impl UnwindSafe for Number
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more