Struct FriendlyProbability

Source
pub struct FriendlyProbability { /* private fields */ }
Expand description

A probability that can be expressed in a user-friendly form. The friendly_string property is a textual representation of the probability (i.e. “5 out of 7”)

Implementations§

Source§

impl FriendlyProbability

Source

pub fn new<T: Into<Option<String>>>( numerator: u8, denominator: u8, friendly_description: &'static str, friendly_string: T, ) -> FriendlyProbability

Create a new FriendlyProbability. Most of the time you will want to use from_probability() instead.

Source

pub fn numerator(self: &FriendlyProbability) -> u8

Gets the numerator of the FriendlyProbability.

Source

pub fn denominator(self: &FriendlyProbability) -> u8

Gets the denominator of the FriendlyProbability.

Source

pub fn friendly_description(self: &FriendlyProbability) -> &str

Gets the friendly description of the FriendlyProbability. This is a qualitative description of the probability (“Still possible”, “Flip a coin”, “Good chance”, etc.)

Source

pub fn friendly_string(self: &FriendlyProbability) -> &str

Gets the friendly string of the FriendlyProbability. Usually this is the same as “{numerator} in {denominator}”, but if the probability is very small it will instead be “<1 in 100”, and if it’s very large it will be “>99 in 100”

Source

pub fn from_probability(probability: f32) -> FriendlyProbability

Create a FriendlyProbability from an f32.

§Examples
use probability_to_friendly_string::FriendlyProbability;
 
let friendly = FriendlyProbability::from_probability(0.723);
assert_eq!(5, friendly.numerator());
assert_eq!(7, friendly.denominator());
assert_eq!("Good chance", friendly.friendly_description());
assert_eq!("5 in 7", friendly.friendly_string());
 
let friendly = FriendlyProbability::from_probability(0.999);
assert_eq!(">99 in 100", friendly.friendly_string());
 
let friendly = FriendlyProbability::from_probability(0.001);
assert_eq!("<1 in 100", friendly.friendly_string());
§Panics

If probability is less than 0.0 or greater than 1.0.

Trait Implementations§

Source§

impl Debug for FriendlyProbability

Source§

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

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

impl Display for FriendlyProbability

Source§

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

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

impl PartialEq for FriendlyProbability

Source§

fn eq(&self, other: &FriendlyProbability) -> 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 Eq for FriendlyProbability

Source§

impl StructuralPartialEq for FriendlyProbability

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> 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<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.