[][src]Struct probability_to_friendly_string::FriendlyProbability

pub struct FriendlyProbability { /* fields omitted */ }

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

impl FriendlyProbability[src]

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

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

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

Gets the numerator of the FriendlyProbability.

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

Gets the denominator of the FriendlyProbability.

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

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

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

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"

pub fn from_probability(probability: f32) -> FriendlyProbability[src]

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

impl Debug for FriendlyProbability[src]

impl Display for FriendlyProbability[src]

impl Eq for FriendlyProbability[src]

impl PartialEq<FriendlyProbability> for FriendlyProbability[src]

impl StructuralEq for FriendlyProbability[src]

impl StructuralPartialEq for FriendlyProbability[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.