pub struct Percent(_, _);Expand description
Human readable percentage.
Percent::from input can be:
The default Percent::from implementation will print 2 decimal numbers.
Anything lower than 0.01 is rounded down to 0.00.
This can be changed by using different functions when initially
creating the Percent, or converting an existing Percent, for example:
let f0 = Percent::new_0(3.0);
let f2 = Percent::from(3.0);
let f3 = Percent::new_3(3.0);
let f4 = Percent::new_4(3.0);
assert!(f0 == "3%");
assert!(f2 == "3.00%");
assert!(f3 == "3.000%");
assert!(f4 == "3.0000%");Cloning
Clone may be expensive:
// Probably cheap (stack allocated string).
let a = Percent::from(100.0);
let b = a.clone();
// Probably expensive (heap allocated string).
let a = Percent::from(f64::MAX);
let b = a.clone();The actual string used internally is not a String,
but a CompactString so that any string 24 bytes (12 bytes on 32-bit) or less are stack allocated instead of heap allocated.
The documentation will still refer to the inner string as a String. Anything returned will also be a String.
Float Errors
- Inputting
f64::NAN,f64::INFINITY,f64::NEG_INFINITYor thef32variants returns errors
To disable checks for these, (you are sure you don’t have NaN’s), enable the ignore_nan_inf feature flag.
Math
These operators are overloaded. They will always output a new Self:
Add +Sub -Div /Mul *Rem %
They can either be:
- Combined with another
Self:Percent::from(1.0) + Percent::from(1.0) - Or with the inner number itself:
Percent::from(1.0) + 1.0
They also have the same panic!() behavior on overflow as the normal ones, because internally,
it is just calling .inner() $OPERATOR $NUMBER.
assert!(Percent::from(10.0) + 10.0 == Percent::from(20.0));
assert!(Percent::from(10.0) - 10.0 == Percent::from(0.0));
assert!(Percent::from(10.0) / 10.0 == Percent::from(1.0));
assert!(Percent::from(10.0) * 10.0 == Percent::from(100.0));
assert!(Percent::from(10.0) % 10.0 == Percent::from(0.0));Overflow example (floats don’t panic in this case):
let n = Percent::from(f64::MAX) + f64::MAX;
assert!(n.inner().is_infinite());Examples
assert!(Percent::zero() == "0.00%");
assert!(Percent::unknown() == "?.??%");
assert!(Percent::from(0.001) == "0.00%");
assert!(Percent::from(0.1) == "0.10%");
assert!(Percent::from(1.0) == "1.00%");
assert!(Percent::from(100.0) == "100.00%");
assert!(Percent::from(1_000.0) == "1,000.00%");
assert!(Percent::from(1_u32) == "1.00%");
assert!(Percent::from(1_000_u32) == "1,000.00%");
assert!(Percent::from(10_000_u32) == "10,000.00%");
assert!(Percent::from(-1_i32) == "-1.00%");
assert!(Percent::from(-1_000_i32) == "-1,000.00%");
assert!(Percent::from(-10_000_i32) == "-10,000.00%");Implementations§
source§impl Percent
impl Percent
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the valid byte slice of the inner String
These bytes can always safely be used for std::str::from_utf8_unchecked.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
sourcepub fn head_dot(&self, len: usize) -> String
pub fn head_dot(&self, len: usize) -> String
Same as Self::head but returns a String ending with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_dot(4) == "2021...");sourcepub fn tail_dot(&self, len: usize) -> String
pub fn tail_dot(&self, len: usize) -> String
Same as Self::tail but returns a String starting with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.tail_dot(5) == "...12-11");sourcepub fn head_tail(&self, head: usize, tail: usize) -> String
pub fn head_tail(&self, head: usize, tail: usize) -> String
Return the first head bytes and last tail
bytes of this string separated with ....
Since all readable types happen to only contain
ASCII characters, all char’s are equal to 1 byte.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_tail(3, 2) == "202...11");
assert!(date.head_tail(3, 3) == "202...-11");
assert!(date.head_tail(3, 5) == "202...12-11");sourcepub fn inf() -> Self
pub fn inf() -> Self
Returns a Self with the f64 value of f64::INFINITY.
The String is set to ∞.
sourcepub fn new_0(f: f64) -> Self
pub fn new_0(f: f64) -> Self
Same as Self::from but with no floating point on the inner String.
The inner f64 stays the same as the input.
This does not round up or down, it completely ignores the floating point.
Examples
| Input | String Output |
|---|---|
| 0.0 | 0% |
| 50.123 | 50% |
| 100.1 | 100% |
sourcepub fn new_1(f: f64) -> Self
pub fn new_1(f: f64) -> Self
Same as Percent::from but with 1 floating point.
sourcepub fn new_3(f: f64) -> Self
pub fn new_3(f: f64) -> Self
Same as Percent::from but with 3 floating point.
sourcepub fn new_4(f: f64) -> Self
pub fn new_4(f: f64) -> Self
Same as Percent::from but with 4 floating point.
sourcepub fn new_5(f: f64) -> Self
pub fn new_5(f: f64) -> Self
Same as Percent::from but with 5 floating point.
sourcepub fn new_6(f: f64) -> Self
pub fn new_6(f: f64) -> Self
Same as Percent::from but with 6 floating point.
sourcepub fn new_7(f: f64) -> Self
pub fn new_7(f: f64) -> Self
Same as Percent::from but with 7 floating point.
sourcepub fn new_8(f: f64) -> Self
pub fn new_8(f: f64) -> Self
Same as Percent::from but with 8 floating point.
sourcepub fn new_9(f: f64) -> Self
pub fn new_9(f: f64) -> Self
Same as Percent::from but with 9 floating point.
sourcepub fn new_10(f: f64) -> Self
pub fn new_10(f: f64) -> Self
Same as Percent::from but with 10 floating point.
sourcepub fn new_11(f: f64) -> Self
pub fn new_11(f: f64) -> Self
Same as Percent::from but with 11 floating point.
sourcepub fn new_12(f: f64) -> Self
pub fn new_12(f: f64) -> Self
Same as Percent::from but with 12 floating point.
sourcepub fn new_13(f: f64) -> Self
pub fn new_13(f: f64) -> Self
Same as Percent::from but with 13 floating point.
sourcepub fn new_14(f: f64) -> Self
pub fn new_14(f: f64) -> Self
Same as Percent::from but with 14 floating point.
Trait Implementations§
source§impl<'__de> BorrowDecode<'__de> for Percent
impl<'__de> BorrowDecode<'__de> for Percent
source§fn borrow_decode<__D: BorrowDecoder<'__de>>(
decoder: &mut __D
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>
source§impl<'de> Deserialize<'de> for Percent
impl<'de> Deserialize<'de> for Percent
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl PartialEq<&Percent> for f64
impl PartialEq<&Percent> for f64
source§impl PartialEq<&Percent> for str
impl PartialEq<&Percent> for str
source§impl PartialEq<&str> for Percent
impl PartialEq<&str> for Percent
source§impl PartialEq<Percent> for Percent
impl PartialEq<Percent> for Percent
source§impl PartialEq<Percent> for f64
impl PartialEq<Percent> for f64
source§impl PartialEq<Percent> for str
impl PartialEq<Percent> for str
source§impl PartialOrd<&Percent> for f64
impl PartialOrd<&Percent> for f64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<&Percent> for str
impl PartialOrd<&Percent> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<&str> for Percent
impl PartialOrd<&str> for Percent
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Percent> for Percent
impl PartialOrd<Percent> for Percent
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Percent> for f64
impl PartialOrd<Percent> for f64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Percent> for str
impl PartialOrd<Percent> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<f64> for &Percent
impl PartialOrd<f64> for &Percent
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<f64> for Percent
impl PartialOrd<f64> for Percent
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<str> for Percent
impl PartialOrd<str> for Percent
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl StructuralPartialEq for Percent
Auto Trait Implementations§
impl RefUnwindSafe for Percent
impl Send for Percent
impl Sync for Percent
impl Unpin for Percent
impl UnwindSafe for Percent
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
source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere T: Display,
source§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more