pub struct Percent(/* private fields */);Expand description
A share between 0 and 100 percent, never NaN.
Because the value is never NaN, Percent is Eq, Ord, and Hash, so it
sorts and compares without surprises. Display prints one decimal place
(32.8%) and honours a precision ({:.0} gives 33%).
§Examples
use sys_lib::Percent;
let p = Percent::from_ratio(41, 125);
assert_eq!(p.to_string(), "32.8%");
assert_eq!(format!("{p:.0}"), "33%");
assert_eq!(p.complement().to_string(), "67.2%");
assert_eq!(Percent::from_ratio(5, 0), Percent::ZERO);
assert_eq!(Percent::from_ratio(9, 4), Percent::FULL);
assert_eq!(Percent::from_value(f32::NAN), Percent::ZERO);
assert!(Percent::FULL > p);Implementations§
Source§impl Percent
impl Percent
Sourcepub fn from_ratio(part: u64, whole: u64) -> Percent
pub fn from_ratio(part: u64, whole: u64) -> Percent
part as a share of whole, exact to 0.1%: clamped to 100%, and 0% when
whole is zero.
Sourcepub fn from_value(value: f32) -> Percent
pub fn from_value(value: f32) -> Percent
value percent, clamped to 0..=100; NaN gives 0%.
Sourcepub fn complement(self) -> Percent
pub fn complement(self) -> Percent
100% minus this.
Trait Implementations§
impl Copy for Percent
impl Eq for Percent
Source§impl Ord for Percent
impl Ord for Percent
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Auto Trait Implementations§
impl Forget for Percent
impl Freeze for Percent
impl RefUnwindSafe for Percent
impl Send for Percent
impl Sync for Percent
impl Unpin for Percent
impl UnsafeUnpin 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
Mutably borrows from an owned value. Read more