pub struct Rational { /* private fields */ }Expand description
An arbitrary-precision rational number.
This type is used to represent numbers in the form a / b where a and b
are Ints and b is non-zero.
Implementations
sourceimpl Rational
 
impl Rational
sourcepub fn new(n: Int, d: Int) -> Rational
 
pub fn new(n: Int, d: Int) -> Rational
Creates a new Rational from a numerator n and denominator d.
This method normalizes the Rational.
Panics
Panics if d is zero.
sourcepub fn into_parts(self) -> (Int, Int)
 
pub fn into_parts(self) -> (Int, Int)
Returns the (numerator, denominator) of this Rational in normalized form.
sourcepub fn normalized(&self) -> bool
 
pub fn normalized(&self) -> bool
Returns whether or not this Rational is normalized.
sourcepub fn normalize(&mut self)
 
pub fn normalize(&mut self)
Normalize this Rational in place.
This method ensures that the Rational is represented in lowest forms, i.e. as a/b such
that a and b share no common factors. It also ensures that the denominator is positive.
Normalizing rationals results in faster calculations, ensuring that the numerator and denominator are as small as possible.
Most of the time, this method is called automatically.
sourcepub fn round(self) -> Int
 
pub fn round(self) -> Int
Consumes this Rational, rounding it to the nearest Int.
This method will round half values away from zero, similarly to f64::round.
Trait Implementations
sourceimpl<'a> AddAssign<&'a Int> for Rational
 
impl<'a> AddAssign<&'a Int> for Rational
sourcefn add_assign(&mut self, other: &'a Int)
 
fn add_assign(&mut self, other: &'a Int)
Performs the += operation. Read more
sourceimpl<'a> AddAssign<&'a Rational> for Rational
 
impl<'a> AddAssign<&'a Rational> for Rational
sourcefn add_assign(&mut self, other: &'a Rational)
 
fn add_assign(&mut self, other: &'a Rational)
Performs the += operation. Read more
sourceimpl AddAssign<Int> for Rational
 
impl AddAssign<Int> for Rational
sourcefn add_assign(&mut self, other: Int)
 
fn add_assign(&mut self, other: Int)
Performs the += operation. Read more
sourceimpl AddAssign<Rational> for Rational
 
impl AddAssign<Rational> for Rational
sourcefn add_assign(&mut self, other: Rational)
 
fn add_assign(&mut self, other: Rational)
Performs the += operation. Read more
sourceimpl<'a> DivAssign<&'a Int> for Rational
 
impl<'a> DivAssign<&'a Int> for Rational
sourcefn div_assign(&mut self, other: &'a Int)
 
fn div_assign(&mut self, other: &'a Int)
Performs the /= operation. Read more
sourceimpl<'a> DivAssign<&'a Rational> for Rational
 
impl<'a> DivAssign<&'a Rational> for Rational
sourcefn div_assign(&mut self, other: &'a Rational)
 
fn div_assign(&mut self, other: &'a Rational)
Performs the /= operation. Read more
sourceimpl DivAssign<Int> for Rational
 
impl DivAssign<Int> for Rational
sourcefn div_assign(&mut self, other: Int)
 
fn div_assign(&mut self, other: Int)
Performs the /= operation. Read more
sourceimpl DivAssign<Rational> for Rational
 
impl DivAssign<Rational> for Rational
sourcefn div_assign(&mut self, other: Rational)
 
fn div_assign(&mut self, other: Rational)
Performs the /= operation. Read more
sourceimpl FromStr for Rational
 
impl FromStr for Rational
type Err = ParseRationalError
type Err = ParseRationalError
The associated error which can be returned from parsing.
sourceimpl<'a> MulAssign<&'a Int> for Rational
 
impl<'a> MulAssign<&'a Int> for Rational
sourcefn mul_assign(&mut self, other: &'a Int)
 
fn mul_assign(&mut self, other: &'a Int)
Performs the *= operation. Read more
sourceimpl<'a> MulAssign<&'a Rational> for Rational
 
impl<'a> MulAssign<&'a Rational> for Rational
sourcefn mul_assign(&mut self, other: &'a Rational)
 
fn mul_assign(&mut self, other: &'a Rational)
Performs the *= operation. Read more
sourceimpl MulAssign<Int> for Rational
 
impl MulAssign<Int> for Rational
sourcefn mul_assign(&mut self, other: Int)
 
fn mul_assign(&mut self, other: Int)
Performs the *= operation. Read more
sourceimpl MulAssign<Rational> for Rational
 
impl MulAssign<Rational> for Rational
sourcefn mul_assign(&mut self, other: Rational)
 
fn mul_assign(&mut self, other: Rational)
Performs the *= operation. Read more
sourceimpl Ord for Rational
 
impl Ord for Rational
sourceimpl PartialOrd<Int> for Rational
 
impl PartialOrd<Int> for Rational
sourcefn partial_cmp(&self, other: &Int) -> Option<Ordering>
 
fn partial_cmp(&self, other: &Int) -> 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
sourceimpl PartialOrd<Rational> for Rational
 
impl PartialOrd<Rational> for Rational
sourcefn partial_cmp(&self, other: &Rational) -> Option<Ordering>
 
fn partial_cmp(&self, other: &Rational) -> 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
sourceimpl PartialOrd<Rational> for Int
 
impl PartialOrd<Rational> for Int
sourcefn partial_cmp(&self, other: &Rational) -> Option<Ordering>
 
fn partial_cmp(&self, other: &Rational) -> 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
sourceimpl<'a> SubAssign<&'a Rational> for Rational
 
impl<'a> SubAssign<&'a Rational> for Rational
sourcefn sub_assign(&mut self, other: &'a Rational)
 
fn sub_assign(&mut self, other: &'a Rational)
Performs the -= operation. Read more
sourceimpl SubAssign<Rational> for Rational
 
impl SubAssign<Rational> for Rational
sourcefn sub_assign(&mut self, other: Rational)
 
fn sub_assign(&mut self, other: Rational)
Performs the -= operation. Read more
impl Eq for Rational
Auto Trait Implementations
impl RefUnwindSafe for Rational
impl Send for Rational
impl Sync for Rational
impl Unpin for Rational
impl UnwindSafe for Rational
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
sourceimpl<T> ToOwned for T where
    T: Clone, 
 
impl<T> ToOwned for T where
    T: Clone, 
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
 
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more