pub struct PhoneNumberUtil { /* private fields */ }Expand description
The main struct for all phone number-related operations.
It encapsulates the library’s core logic and provides a public API for parsing, formatting, and validating phone numbers. An instance of this struct is the primary entry point for using the library’s features.
Implementations§
Source§impl PhoneNumberUtil
impl PhoneNumberUtil
Sourcepub fn can_be_internationally_dialled(&self, phone_number: &PhoneNumber) -> bool
pub fn can_be_internationally_dialled(&self, phone_number: &PhoneNumber) -> bool
Checks if a PhoneNumber can be dialed internationally.
§Parameters
phone_number: A reference to thePhoneNumberobject to be checked.
§Returns
true if the number can be dialed from another country, false otherwise.
§Panics
This method panics if the underlying metadata contains an invalid regular expression, which indicates a critical library bug.
Sourcepub fn convert_alpha_characters_in_number<'a>(
&self,
number: impl AsRef<str>,
) -> String
pub fn convert_alpha_characters_in_number<'a>( &self, number: impl AsRef<str>, ) -> String
Converts all alpha characters in a phone number string to their corresponding digits.
For example, an input of “1-800-FLOWERS” will be converted to “1-800-3569377”.
§Parameters
number: A string slice orStringrepresenting the phone number.
§Returns
A String containing the phone number with all alphabetic characters converted to digits.
Sourcepub fn format<'a>(
&self,
phone_number: &'a PhoneNumber,
number_format: PhoneNumberFormat,
) -> Cow<'a, str>
pub fn format<'a>( &self, phone_number: &'a PhoneNumber, number_format: PhoneNumberFormat, ) -> Cow<'a, str>
Formats a PhoneNumber into a standardized format.
§Parameters
phone_number: ThePhoneNumberto be formatted.number_format: ThePhoneNumberFormatto be applied (e.g., E164, INTERNATIONAL, NATIONAL).
§Returns
A Cow<'a, str> which is either a borrowed reference to a pre-formatted string or a
newly allocated String with the formatted number.
§Panics
This method panics if the underlying metadata contains an invalid regular expression, indicating a library bug.
Sourcepub fn format_in_original_format<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
) -> Cow<'a, str>
pub fn format_in_original_format<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, ) -> Cow<'a, str>
Formats a PhoneNumber, attempting to preserve original formatting and punctuation.
The number is formatted in the national format of the region it is from.
§Parameters
phone_number: ThePhoneNumberto be formatted.region_calling_from: The two-letter region code (ISO 3166-1) from where the call is being made.
§Returns
A Cow<'a, str> containing the formatted number.
§Panics
This method panics if metadata is invalid, which indicates a library bug.
Sourcepub fn format_national_number_with_carrier_code<'a>(
&self,
phone_number: &'a PhoneNumber,
carrier_code: impl AsRef<str>,
) -> String
pub fn format_national_number_with_carrier_code<'a>( &self, phone_number: &'a PhoneNumber, carrier_code: impl AsRef<str>, ) -> String
Sourcepub fn format_number_for_mobile_dialing<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
with_formatting: bool,
) -> Cow<'a, str>
pub fn format_number_for_mobile_dialing<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, with_formatting: bool, ) -> Cow<'a, str>
Formats a PhoneNumber for dialing from a mobile device.
§Parameters
phone_number: ThePhoneNumberto format.region_calling_from: The two-letter region code (ISO 3166-1) where the user is.with_formatting: Iftrue, the number is formatted with punctuation; otherwise, only digits are returned.
§Returns
A Cow<'a, str> with the dialable number.
§Panics
Panics if formatting fails due to a library bug.
Sourcepub fn format_out_of_country_calling_number<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
) -> Cow<'a, str>
pub fn format_out_of_country_calling_number<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, ) -> Cow<'a, str>
Formats a PhoneNumber for out-of-country calling.
§Parameters
phone_number: ThePhoneNumberto format.region_calling_from: The two-letter region code (ISO 3166-1) of the calling location.
§Returns
A Cow<'a, str> representing the number formatted for international dialing.
§Panics
Panics on invalid metadata, indicating a library bug.
Sourcepub fn format_out_of_country_keeping_alpha_chars<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
) -> Cow<'a, str>
pub fn format_out_of_country_keeping_alpha_chars<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, ) -> Cow<'a, str>
Formats a PhoneNumber for out-of-country calling while preserving any alphabetic characters.
§Parameters
phone_number: ThePhoneNumberto format.region_calling_from: The two-letter region code (ISO 3166-1) of the calling location.
§Returns
A Cow<'a, str> with the formatted number.
§Panics
Panics on invalid metadata, indicating a library bug.
Sourcepub fn get_example_number(
&self,
region_code: impl AsRef<str>,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_example_number( &self, region_code: impl AsRef<str>, ) -> Result<PhoneNumber, GetExampleNumberError>
Sourcepub fn get_example_number_for_type(
&self,
number_type: PhoneNumberType,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_example_number_for_type( &self, number_type: PhoneNumberType, ) -> Result<PhoneNumber, GetExampleNumberError>
Sourcepub fn get_invalid_example_number(
&self,
region_code: impl AsRef<str>,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_invalid_example_number( &self, region_code: impl AsRef<str>, ) -> Result<PhoneNumber, GetExampleNumberError>
Sourcepub fn get_length_of_geographical_area_code(
&self,
phone_number: &PhoneNumber,
) -> usize
pub fn get_length_of_geographical_area_code( &self, phone_number: &PhoneNumber, ) -> usize
Sourcepub fn get_length_of_national_destination_code(
&self,
phone_number: &PhoneNumber,
) -> usize
pub fn get_length_of_national_destination_code( &self, phone_number: &PhoneNumber, ) -> usize
Sourcepub fn get_national_significant_number<'a>(
&self,
phone_number: &'a PhoneNumber,
) -> String
pub fn get_national_significant_number<'a>( &self, phone_number: &'a PhoneNumber, ) -> String
Sourcepub fn get_number_type(&self, phone_number: &PhoneNumber) -> PhoneNumberType
pub fn get_number_type(&self, phone_number: &PhoneNumber) -> PhoneNumberType
Sourcepub fn get_region_code_for_country_code(&self, country_code: i32) -> &str
pub fn get_region_code_for_country_code(&self, country_code: i32) -> &str
Gets the primary region code for a given country calling code.
Note: Some country codes are shared by multiple regions (e.g., +1 for USA, Canada). This returns the main region for that code (e.g., “US” for +1).
§Parameters
country_code: The country calling code.
§Returns
A string slice with the corresponding two-letter region code. Returns “ZZ” for invalid codes.
Sourcepub fn get_region_code_for_number(&self, phone_number: &PhoneNumber) -> &str
pub fn get_region_code_for_number(&self, phone_number: &PhoneNumber) -> &str
Sourcepub fn get_region_codes_for_country_code(
&self,
country_code: i32,
) -> Option<impl ExactSizeIterator<Item = &str>>
pub fn get_region_codes_for_country_code( &self, country_code: i32, ) -> Option<impl ExactSizeIterator<Item = &str>>
Sourcepub fn get_supported_regions(&self) -> impl ExactSizeIterator<Item = &str>
pub fn get_supported_regions(&self) -> impl ExactSizeIterator<Item = &str>
Gets an iterator over all supported two-letter region codes.
§Returns
An ExactSizeIterator that yields string slices of all supported region codes.
Sourcepub fn is_alpha_number(&self, number: impl AsRef<str>) -> bool
pub fn is_alpha_number(&self, number: impl AsRef<str>) -> bool
Sourcepub fn is_nanpa_country(&self, region_code: impl AsRef<str>) -> bool
pub fn is_nanpa_country(&self, region_code: impl AsRef<str>) -> bool
Sourcepub fn is_number_geographical(&self, phone_number: &PhoneNumber) -> bool
pub fn is_number_geographical(&self, phone_number: &PhoneNumber) -> bool
Sourcepub fn is_number_match(
&self,
first_number: &PhoneNumber,
second_number: &PhoneNumber,
) -> MatchType
pub fn is_number_match( &self, first_number: &PhoneNumber, second_number: &PhoneNumber, ) -> MatchType
Sourcepub fn is_possible_number(&self, phone_number: &PhoneNumber) -> bool
pub fn is_possible_number(&self, phone_number: &PhoneNumber) -> bool
Sourcepub fn is_possible_number_with_reason(
&self,
phone_number: &PhoneNumber,
) -> Result<NumberLengthType, ValidationError>
pub fn is_possible_number_with_reason( &self, phone_number: &PhoneNumber, ) -> Result<NumberLengthType, ValidationError>
Sourcepub fn is_valid_number(&self, phone_number: &PhoneNumber) -> bool
pub fn is_valid_number(&self, phone_number: &PhoneNumber) -> bool
Sourcepub fn is_valid_number_for_region(
&self,
phone_number: &PhoneNumber,
region: impl AsRef<str>,
) -> bool
pub fn is_valid_number_for_region( &self, phone_number: &PhoneNumber, region: impl AsRef<str>, ) -> bool
Sourcepub fn parse_and_keep_raw_input(
&self,
number_to_parse: impl AsRef<str>,
default_region: impl AsRef<str>,
) -> Result<PhoneNumber, ParseError>
pub fn parse_and_keep_raw_input( &self, number_to_parse: impl AsRef<str>, default_region: impl AsRef<str>, ) -> Result<PhoneNumber, ParseError>
Parses a string into a PhoneNumber, keeping the raw input string.
§Parameters
number_to_parse: The phone number string.default_region: The two-letter region code (ISO 3166-1) to use if the number is not in international format.
§Returns
A Result containing the parsed PhoneNumber on success, or a ParseError on failure.
Sourcepub fn parse(
&self,
number_to_parse: impl AsRef<str>,
default_region: impl AsRef<str>,
) -> Result<PhoneNumber, ParseError>
pub fn parse( &self, number_to_parse: impl AsRef<str>, default_region: impl AsRef<str>, ) -> Result<PhoneNumber, ParseError>
Parses a string into a PhoneNumber.
This is the primary method for converting a string representation of a number
into a structured PhoneNumber object.
§Parameters
number_to_parse: The phone number string.default_region: The two-letter region code (ISO 3166-1) to use if the number is not in international format.
§Returns
A Result containing the parsed PhoneNumber on success, or a ParseError on failure.