Struct icu::plurals::PluralRules

source ·
pub struct PluralRules(_);
Expand description

A struct which provides an ability to retrieve an appropriate Plural Category for a given number.

Examples

use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRuleType, PluralRules};

let pr = PluralRules::try_new_unstable(
    &icu_testdata::unstable(),
    &locale!("en").into(),
    PluralRuleType::Cardinal,
)
.expect("Failed to construct a PluralRules struct.");

assert_eq!(pr.category_for(5_usize), PluralCategory::Other);

Implementations§

source§

impl PluralRules

source

pub fn try_new_unstable<D>( data_provider: &D, locale: &DataLocale, rule_type: PluralRuleType ) -> Result<PluralRules, PluralsError>where D: DataProvider<CardinalV1Marker> + DataProvider<OrdinalV1Marker> + ?Sized,

Constructs a new PluralRules for a given locale, type and data provider.

This constructor will fail if the Data Provider does not have the data.

📚 Help choosing a constructor

⚠️ The bounds on this function may change over time, including in SemVer minor releases.
Examples
use icu::locid::locale;
use icu::plurals::{PluralRuleType, PluralRules};

let _ = PluralRules::try_new_unstable(
    &icu_testdata::unstable(),
    &locale!("en").into(),
    PluralRuleType::Cardinal,
);
source

pub fn try_new_with_any_provider( provider: &impl AnyProvider, locale: &DataLocale, rule_type: PluralRuleType ) -> Result<PluralRules, PluralsError>

Creates a new instance using an AnyProvider.

For details on the behavior of this function, see: Self::try_new_unstable

📚 Help choosing a constructor

source

pub fn try_new_with_buffer_provider( provider: &impl BufferProvider, locale: &DataLocale, rule_type: PluralRuleType ) -> Result<PluralRules, PluralsError>

Enabled with the "serde" feature.

Creates a new instance using a BufferProvider.

For details on the behavior of this function, see: Self::try_new_unstable

📚 Help choosing a constructor

source

pub fn try_new_cardinal_unstable<D>( data_provider: &D, locale: &DataLocale ) -> Result<PluralRules, PluralsError>where D: DataProvider<CardinalV1Marker> + ?Sized,

Constructs a new PluralRules for a given locale for cardinal numbers.

Cardinal plural forms express quantities of units such as time, currency or distance, used in conjunction with a number expressed in decimal digits (i.e. “2”, not “two”).

For example, English has two forms for cardinals:

  • One: 1 day
  • Other: 0 days, 2 days, 10 days, 0.3 days

📚 Help choosing a constructor

⚠️ The bounds on this function may change over time, including in SemVer minor releases.
Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRules};

let rules = PluralRules::try_new_cardinal_unstable(
    &icu_testdata::unstable(),
    &locale!("ru").into(),
)
.expect("Data should be present");

assert_eq!(rules.category_for(2_usize), PluralCategory::Few);
source

pub fn try_new_cardinal_with_any_provider( provider: &impl AnyProvider, locale: &DataLocale ) -> Result<PluralRules, PluralsError>

Creates a new instance using an AnyProvider.

For details on the behavior of this function, see: Self::try_new_cardinal_unstable

📚 Help choosing a constructor

source

pub fn try_new_cardinal_with_buffer_provider( provider: &impl BufferProvider, locale: &DataLocale ) -> Result<PluralRules, PluralsError>

Enabled with the "serde" feature.

Creates a new instance using a BufferProvider.

For details on the behavior of this function, see: Self::try_new_cardinal_unstable

📚 Help choosing a constructor

source

pub fn try_new_ordinal_unstable<D>( data_provider: &D, locale: &DataLocale ) -> Result<PluralRules, PluralsError>where D: DataProvider<OrdinalV1Marker> + ?Sized,

Constructs a new PluralRules for a given locale for ordinal numbers.

Ordinal plural forms denote the order of items in a set and are always integers.

For example, English has four forms for ordinals:

  • One: 1st floor, 21st floor, 101st floor
  • Two: 2nd floor, 22nd floor, 102nd floor
  • Few: 3rd floor, 23rd floor, 103rd floor
  • Other: 4th floor, 11th floor, 96th floor

📚 Help choosing a constructor

⚠️ The bounds on this function may change over time, including in SemVer minor releases.
Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRules};

let rules = PluralRules::try_new_ordinal_unstable(
    &icu_testdata::unstable(),
    &locale!("ru").into(),
)
.expect("Data should be present");

assert_eq!(rules.category_for(2_usize), PluralCategory::Other);
source

pub fn try_new_ordinal_with_any_provider( provider: &impl AnyProvider, locale: &DataLocale ) -> Result<PluralRules, PluralsError>

Creates a new instance using an AnyProvider.

For details on the behavior of this function, see: Self::try_new_ordinal_unstable

📚 Help choosing a constructor

source

pub fn try_new_ordinal_with_buffer_provider( provider: &impl BufferProvider, locale: &DataLocale ) -> Result<PluralRules, PluralsError>

Enabled with the "serde" feature.

Creates a new instance using a BufferProvider.

For details on the behavior of this function, see: Self::try_new_ordinal_unstable

📚 Help choosing a constructor

source

pub fn category_for<I>(&self, input: I) -> PluralCategorywhere I: Into<PluralOperands>,

Returns the Plural Category appropriate for the given number.

Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRuleType, PluralRules};

let pr = PluralRules::try_new_unstable(
    &icu_testdata::unstable(),
    &locale!("en").into(),
    PluralRuleType::Cardinal,
)
.expect("Failed to construct a PluralRules struct.");

match pr.category_for(1_usize) {
    PluralCategory::One => "One item",
    PluralCategory::Other => "Many items",
    _ => unreachable!(),
};

The method accepts any input that can be calculated into Plural Operands. All unsigned primitive number types can infallibly be converted so they can be used as an input.

For signed numbers and strings, Plural Operands implement TryFrom and FromStr, which should be used before passing the result to category_for().

Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralOperands};
use icu::plurals::{PluralRuleType, PluralRules};
use std::convert::TryFrom;

let operands = PluralOperands::try_from(-5).expect("Failed to parse to operands.");
let operands2: PluralOperands = "5.10".parse().expect("Failed to parse to operands.");

assert_eq!(pr.category_for(operands), PluralCategory::Other);
assert_eq!(pr.category_for(operands2), PluralCategory::Other);
source

pub fn categories(&self) -> impl Iterator<Item = PluralCategory>

Returns all Plural Categories appropriate for a PluralRules object based on the LanguageIdentifier and PluralRuleType.

The Plural Categories are returned in UTS 35 sorted order.

The category PluralCategory::Other is always included.

Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRuleType, PluralRules};

let pr = PluralRules::try_new_unstable(
    &icu_testdata::unstable(),
    &locale!("fr").into(),
    PluralRuleType::Cardinal,
)
.expect("Failed to construct a PluralRules struct.");

let mut categories = pr.categories();
assert_eq!(categories.next(), Some(PluralCategory::One));
assert_eq!(categories.next(), Some(PluralCategory::Many));
assert_eq!(categories.next(), Some(PluralCategory::Other));
assert_eq!(categories.next(), None);

Trait Implementations§

source§

impl Debug for PluralRules

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for Twhere T: 'static,

source§

impl<T> MaybeSendSync for T