Skip to content

Validate phone numbers in Rust

One file that reads a number the way people type it, checks it against the numbering plans you pick, names its line type, writes it as E.164 and as a tel: link, and says what to dial from another country. The patterns are the plans' own. The tests come with it, and the code on this page was run before it was published.

Files
src/phone.rs, tests in src/phone.rs (the end of the file)
Needs
Rust 1.80 or later; the regex crate
Tests
cargo test
Plans as of
2026-10-25

What it answers

CallWith the United Kingdom's plan loaded
parse “07400 123456”, default region GB+447400123456, GB, mobile
teltel:+447400123456
dial_from from US011447400123456
parse “07400 12345”None: no range of the plan has that length

The plans in this file

Add the countries whose numbers you accept; the file and its tests below are generated again for them, in your browser.

  • United Kingdom +44
  • United States +1
  • France +33

1. The plans

PLANS holds one entry per plan: its region (the ISO code), the country code, the trunk prefix dialled before a national number, the exit code dialled before an international one, and a pattern per line type. The patterns are the plans' own, anchored, in the order the file tries them: the special ranges first (premium rate, toll-free, shared cost, VoIP, personal, pager, UAN, voicemail), then fixed line, then mobile. A plan that names no single exit code gets an empty one, and calls out of it use the + form.

//! phone.rs: validate, normalise and dial phone numbers with the national numbering plans.
//! Plans: GB +44, US +1, FR +33 (data as of 2026-10-25).
//! Other plans: https://phonelookup.dev/guides/validate-phone-numbers-rust?plans=gb,us,fr
//! Each pattern matches a national significant number: digits only, no country code, no trunk prefix.
//!
//! Needs the regex crate: `cargo add regex`.

use regex::Regex;
use std::sync::LazyLock;

/// One numbering plan: region, country code, trunk prefix, exit code and an anchored pattern per line type.
pub struct Plan {
    pub region: &'static str,
    pub cc: &'static str,
    pub trunk: &'static str,
    pub exit: &'static str,
    pub types: Vec<(&'static str, Regex)>,
}

/// A number as read: E.164, the plan it belongs to, its line type, country code and national number.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PhoneNumber {
    pub e164: String,
    pub region: &'static str,
    pub kind: &'static str,
    pub cc: &'static str,
    pub nsn: String,
}

fn line_type(name: &'static str, pattern: &str) -> (&'static str, Regex) {
    let anchored = format!("^(?:{pattern})$");
    (name, Regex::new(&anchored).expect("a valid pattern"))
}

/// The numbering plans this module reads, compiled once, on first use.
pub static PLANS: LazyLock<Vec<Plan>> = LazyLock::new(|| {
    vec![
        Plan {
            region: "GB",
            cc: "44",
            trunk: "0",
            exit: "00",
            types: vec![
                line_type("premiumRate", r"(?:8(?:4[2-5]|7[0-3])|9(?:[01]\d|8[2-49]))\d{7}|845464\d"),
                line_type("tollFree", r"80[08]\d{7}|800\d{6}|8001111"),
                line_type("voip", r"56\d{8}"),
                line_type("personalNumber", r"70\d{8}"),
                line_type("pager", r"76(?:464|652)\d{5}|76(?:0[0-28]|2[356]|34|4[01347]|5[49]|6[0-369]|77|8[14]|9[139])\d{6}"),
                line_type("uan", r"(?:3[0347]|55)\d{8}"),
                line_type("fixedLine", r"(?:1(?:1(?:3(?:[0-58]\d\d|73[0-5])|4(?:(?:[0-5]\d|70)\d|69[7-9])|(?:(?:5[0-26-9]|[78][0-49])\d|6(?:[0-4]\d|5[01]))\d)|(?:2(?:(?:0[024-9]|2[3-9]|3[3-79]|4[1-689]|[58][02-9]|6[0-47-9]|7[013-9]|9\d)\d|1(?:[0-7]\d|8[0-3]))|(?:3(?:0\d|1[0-8]|[25][02-9]|3[02-579]|[468][0-46-9]|7[1-35-79]|9[2-578])|4(?:0[03-9]|[137]\d|[28][02-57-9]|4[02-69]|5[0-8]|[69][0-79])|5(?:0[1-35-9]|[16]\d|2[024-9]|3[015689]|4[02-9]|5[03-9]|7[0-35-9]|8[0-468]|9[0-57-9])|6(?:0[034689]|1\d|2[0-35689]|[38][013-9]|4[1-467]|5[0-69]|6[13-9]|7[0-8]|9[0-24578])|7(?:0[0246-9]|2\d|3[0236-8]|4[03-9]|5[0-46-9]|6[013-9]|7[0-35-9]|8[024-9]|9[02-9])|8(?:0[35-9]|2[1-57-9]|3[02-578]|4[0-578]|5[124-9]|6[2-69]|7\d|8[02-9]|9[02569])|9(?:0[02-589]|[18]\d|2[02-689]|3[1-57-9]|4[2-9]|5[0-579]|6[2-47-9]|7[0-24578]|9[2-57]))\d)\d)|2(?:0[013478]|3[0189]|4[017]|8[0-46-9]|9[0-2])\d{3})\d{4}|1(?:2(?:0(?:46[1-4]|87[2-9])|545[1-79]|76(?:2\d|3[1-8]|6[1-6])|9(?:7(?:2[0-4]|3[2-5])|8(?:2[2-8]|7[0-47-9]|8[3-5])))|3(?:6(?:38[2-5]|47[23])|8(?:47[04-9]|64[0157-9]))|4(?:044[1-7]|20(?:2[23]|8\d)|6(?:0(?:30|5[2-57]|6[1-8]|7[2-8])|140)|8(?:052|87[1-3]))|5(?:2(?:4(?:3[2-79]|6\d)|76\d)|6(?:26[06-9]|686))|6(?:06(?:4\d|7[4-79])|295[5-7]|35[34]\d|47(?:24|61)|59(?:5[08]|6[67]|74)|9(?:55[0-4]|77[23]))|7(?:26(?:6[13-9]|7[0-7])|(?:442|688)\d|50(?:2[0-3]|[3-68]2|76))|8(?:27[56]\d|37(?:5[2-5]|8[239])|843[2-58])|9(?:0(?:0(?:6[1-8]|85)|52\d)|3583|4(?:66[1-8]|9(?:2[01]|81))|63(?:23|3[1-4])|9561))\d{3}"),
                line_type("mobile", r"7(?:457[0-57-9]|700[01]|911[028])\d{5}|7(?:[1-3]\d\d|4(?:[0-46-9]\d|5[0-689])|5(?:0[0-8]|[13-9]\d|2[0-35-9])|7(?:0[1-9]|[1-7]\d|8[02-9]|9[0-689])|8(?:[014-9]\d|[23][0-8])|9(?:[024-9]\d|1[02-9]|3[0-689]))\d{6}"),
            ],
        },
        Plan {
            region: "US",
            cc: "1",
            trunk: "1",
            exit: "011",
            types: vec![
                line_type("premiumRate", r"900[2-9]\d{6}"),
                line_type("tollFree", r"8(?:00|33|44|55|66|77|88)[2-9]\d{6}"),
                line_type("personalNumber", r"52(?:3(?:[2-46-9][02-9]\d|5(?:[02-46-9]\d|5[0-46-9]))|4(?:[2-478][02-9]\d|5(?:[034]\d|2[024-9]|5[0-46-9])|6(?:0[1-9]|[2-9]\d)|9(?:[05-9]\d|2[0-5]|49)))\d{4}|52[34][2-9]1[02-9]\d{4}|5(?:00|2[125-9]|3[23]|44|66|77|88)[2-9]\d{6}"),
                line_type("fixedLine", r"983[2-57-9]\d{6}|(?:2(?:0[1-35-9]|1[02-9]|2[03-57-9]|3[1459]|4[08]|5[1-46]|6[0279]|7[02469]|8[13])|3(?:0[1-57-9]|1[02-9]|2[013-79]|3[0-24679]|4[167]|5[0-3]|6[01349]|8[056])|4(?:0[124-9]|1[02-579]|2[3-5]|3[0245]|4[023578]|58|6[349]|7[02589]|8[04])|5(?:0[1-57-9]|1[0235-8]|20|3[0149]|4[01]|5[179]|6[1-47]|7[0-5]|8[0256])|6(?:0[1-35-9]|1[024-9]|2[03689]|3[016]|4[0156]|5[01679]|6[0-279]|78|8[0-269])|7(?:0[1-46-8]|1[2-9]|2[04-8]|3[0-2478]|4[0378]|5[47]|6[02359]|7[0-59]|8[156])|8(?:0[1-68]|1[02-8]|2[0168]|3[0-2589]|4[03578]|5[046-9]|6[02-5]|7[028])|9(?:0[1346-9]|1[02-9]|2[0589]|3[0146-8]|4[01357-9]|5[12469]|7[0-3589]|8[04-69]))[2-9]\d{6}"),
                line_type("mobile", r"983[2-57-9]\d{6}|(?:2(?:0[1-35-9]|1[02-9]|2[03-57-9]|3[1459]|4[08]|5[1-46]|6[0279]|7[02469]|8[13])|3(?:0[1-57-9]|1[02-9]|2[013-79]|3[0-24679]|4[167]|5[0-3]|6[01349]|8[056])|4(?:0[124-9]|1[02-579]|2[3-5]|3[0245]|4[023578]|58|6[349]|7[02589]|8[04])|5(?:0[1-57-9]|1[0235-8]|20|3[0149]|4[01]|5[179]|6[1-47]|7[0-5]|8[0256])|6(?:0[1-35-9]|1[024-9]|2[03689]|3[016]|4[0156]|5[01679]|6[0-279]|78|8[0-269])|7(?:0[1-46-8]|1[2-9]|2[04-8]|3[0-2478]|4[0378]|5[47]|6[02359]|7[0-59]|8[156])|8(?:0[1-68]|1[02-8]|2[0168]|3[0-2589]|4[03578]|5[046-9]|6[02-5]|7[028])|9(?:0[1346-9]|1[02-9]|2[0589]|3[0146-8]|4[01357-9]|5[12469]|7[0-3589]|8[04-69]))[2-9]\d{6}"),
            ],
        },
        Plan {
            region: "FR",
            cc: "33",
            trunk: "0",
            exit: "00",
            types: vec![
                line_type("premiumRate", r"836(?:0[0-36-9]|[1-9]\d)\d{4}|8(?:1[2-9]|2[2-47-9]|3[0-57-9]|[569]\d|8[0-35-9])\d{6}"),
                line_type("tollFree", r"80[0-5]\d{6}"),
                line_type("sharedCost", r"8(?:1[01]|2[0156]|4[024]|84)\d{6}"),
                line_type("voip", r"9\d{8}"),
                line_type("uan", r"80[6-9]\d{6}"),
                line_type("fixedLine", r"(?:26[013-9]|59[1-35-9])\d{6}|(?:[13]\d|2[0-57-9]|4[1-9]|5[0-8])\d{7}"),
                line_type("mobile", r"(?:6(?:[0-24-8]\d|3[0-8]|9[589])|7[3-9]\d)\d{6}"),
            ],
        },
    ]
});

2. Reading a number

parse keeps the digits and a leading plus. With the plus, or with the default region's exit code in front (00 from the United Kingdom, 011 from the United States), the first one to three digits are the country code: under ITU-T E.164 no code is the prefix of another, so at most one length names a plan, and the plans sharing a code (+1, +7, +44) are tried in turn. Without either, the number is national and is read in the default region's plan. The trunk prefix is tried stripped first, so “+44 (0)7400 123456” reads too. A number in both the fixed-line and the mobile ranges comes back as fixedLineOrMobile, which is every ordinary number in the +1 plans. Digits typed in other scripts are dropped with the punctuation; convert them first if your users type them. Anything no plan accepts gives None.

/// Reads a number the way people type it: "+44 7400 123456", "07400 123456", "00 44 7400 123456".
/// A number without + or an exit code is read in the plan of `default_region` (an ISO code such as "GB").
/// None when no plan in [`PLANS`] accepts it.
pub fn parse(text: &str, default_region: Option<&str>) -> Option<PhoneNumber> {
    let s: String = text.chars().filter(|c| c.is_ascii_digit() || *c == '+').collect();
    let digits: String = s.chars().filter(char::is_ascii_digit).collect();
    if !(3..=17).contains(&digits.len()) {
        return None;
    }
    if s.starts_with('+') {
        return international(&digits);
    }
    let home = PLANS.iter().find(|p| Some(p.region) == default_region)?;
    if !home.exit.is_empty() && digits.starts_with(home.exit) {
        return international(&digits[home.exit.len()..]);
    }
    let shared = PLANS.iter().filter(|p| p.cc == home.cc && !std::ptr::eq(*p, home));
    find(std::iter::once(home).chain(shared), &digits)
}

// Country codes are prefix-free (ITU-T E.164): the first one to three digits that name a plan are the code.
fn international(digits: &str) -> Option<PhoneNumber> {
    for size in 1..=digits.len().min(3) {
        let mut plans = PLANS.iter().filter(|p| p.cc == &digits[..size]).peekable();
        if plans.peek().is_some() {
            return find(plans, &digits[size..]);
        }
    }
    None
}

// The trunk prefix is tried stripped first: "+44 (0)7400 123456" is common.
fn find<'a>(plans: impl Iterator<Item = &'a Plan>, nsn: &str) -> Option<PhoneNumber> {
    for plan in plans {
        let mut tries = vec![nsn];
        if !plan.trunk.is_empty() && nsn.starts_with(plan.trunk) {
            tries.insert(0, &nsn[plan.trunk.len()..]);
        }
        for national in tries {
            if let Some(kind) = kind_of(plan, national) {
                return Some(PhoneNumber {
                    e164: format!("+{}{}", plan.cc, national),
                    region: plan.region,
                    kind,
                    cc: plan.cc,
                    nsn: national.to_string(),
                });
            }
        }
    }
    None
}

// The first matching type wins; a number in both the fixed-line and the mobile ranges is reported as such.
fn kind_of(plan: &Plan, nsn: &str) -> Option<&'static str> {
    let hits: Vec<&'static str> = plan
        .types
        .iter()
        .filter(|(_, re)| re.is_match(nsn))
        .map(|(name, _)| *name)
        .collect();
    match hits.first() {
        Some(&"fixedLine") if hits.contains(&"mobile") => Some("fixedLineOrMobile"),
        first => first.copied(),
    }
}

3. Writing and dialling it

Store the E.164 form, a plus and digits only: one string per number, and every other format follows from it.tel writes the tel: URI for links (RFC 3966). dial_from gives the digits a person dials from another country: the caller's exit code, the country code and the national number, or, between plans that share a country code, a national call with the trunk prefix. The dialling helper explains each case.

impl PhoneNumber {
    /// The tel: URI (RFC 3966), for links.
    pub fn tel(&self) -> String {
        format!("tel:{}", self.e164)
    }

    /// What to dial from a phone in `from_region`. Inside one country code (the UK and Jersey, the US and
    /// Canada) the call is dialled like a national one, with the trunk prefix; otherwise the exit code, the
    /// country code and the national number. The + form when the region is not in [`PLANS`] or its plan
    /// names no exit code.
    pub fn dial_from(&self, from_region: &str) -> String {
        match PLANS.iter().find(|p| p.region == from_region) {
            Some(from) if from.cc == self.cc => format!("{}{}", from.trunk, self.nsn),
            Some(from) if !from.exit.is_empty() => format!("{}{}{}", from.exit, self.cc, self.nsn),
            _ => self.e164.clone(),
        }
    }
}

The tests

The cases are the plans' own example numbers, one per line type in international form; the first plan's mobile number again in national form, with “(0)”, and dialled with an exit code; then numbers no plan accepts, and three calls dialled. Their expected values are computed when the page is built, by the same logic in TypeScript. The generator behind this page is checked by running its output in cargo test, for these plans and for all 245 plans at once.

cargo add regex
# mod phone; in src/lib.rs or src/main.rs
cargo test
#[cfg(test)]
mod tests {
    use super::*;

    fn read(text: &str) -> PhoneNumber {
        parse(text, None).unwrap_or_else(|| panic!("{text} not read"))
    }

    #[test]
    fn reads_each_line_type_of_each_plan() {
        let cases: [(&str, Option<&str>, &str, &str); 22] = [
            // typed, default region, E.164, line type
            ("+44 7400 123456", None, "+447400123456", "mobile"),
            ("+44 121 234 5678", None, "+441212345678", "fixedLine"),
            ("+44 800 123 4567", None, "+448001234567", "tollFree"),
            ("+44 56 1234 5678", None, "+445612345678", "voip"),
            ("+44 901 234 5678", None, "+449012345678", "premiumRate"),
            ("+44 70 1234 5678", None, "+447012345678", "personalNumber"),
            ("+44 76 4012 3456", None, "+447640123456", "pager"),
            ("+44 55 1234 5678", None, "+445512345678", "uan"),
            ("+1 201-555-0123", None, "+12015550123", "fixedLineOrMobile"),
            ("+1 800-234-5678", None, "+18002345678", "tollFree"),
            ("+1 900-234-5678", None, "+19002345678", "premiumRate"),
            ("+1 500-234-5678", None, "+15002345678", "personalNumber"),
            ("+33 6 12 34 56 78", None, "+33612345678", "mobile"),
            ("+33 1 23 45 67 89", None, "+33123456789", "fixedLine"),
            ("+33 801 23 45 67", None, "+33801234567", "tollFree"),
            ("+33 9 12 34 56 78", None, "+33912345678", "voip"),
            ("+33 884 01 23 45", None, "+33884012345", "sharedCost"),
            ("+33 891 12 34 56", None, "+33891123456", "premiumRate"),
            ("+33 806 12 34 56", None, "+33806123456", "uan"),
            ("07400 123456", Some("GB"), "+447400123456", "mobile"),
            ("+44 (0)7400 123456", None, "+447400123456", "mobile"),
            ("00 1 201-555-0123", Some("GB"), "+12015550123", "fixedLineOrMobile"),
        ];
        for (text, region, e164, kind) in cases {
            let n = parse(text, region).unwrap_or_else(|| panic!("{text} not read"));
            assert_eq!((n.e164.as_str(), n.kind), (e164, kind), "{text}");
        }
    }

    #[test]
    fn rejects_what_no_plan_accepts() {
        assert_eq!(parse("12", Some("GB")), None);
        assert_eq!(parse("+44 7400 12345", None), None);
        assert_eq!(parse("+49 1512 3456789", None), None);
        assert_eq!(parse("07400 123456", None), None);
    }

    #[test]
    fn writes_and_dials() {
        assert_eq!(read("+44 7400 123456").tel(), "tel:+447400123456");
        assert_eq!(read("+1 201-555-0123").dial_from("GB"), "0012015550123");
        assert_eq!(read("+44 7400 123456").dial_from("US"), "011447400123456");
        assert_eq!(read("+44 7400 123456").dial_from("GB"), "07400123456");
    }
}

What is specific to Rust

  • The standard library has no regular expressions; the regex crate matches in linear time, without backreferences or lookarounds, which the plans' patterns do not use.
  • LazyLock (Rust 1.80) compiles the patterns once, on first use. A Regex::new inside a function rebuilds the automaton on every call.
  • is_match finds a match anywhere: the patterns are compiled anchored, ^(?:…)$. \d is Unicode-aware in the crate; the module only hands it ASCII digits.
  • type is a keyword, so the line type is the field kind.

Sources of these plans: National Telephone Numbering Plan (Ofcom); National numbering plans, ITU-T E.164 (ITU); North American Numbering Plan Administrator (NANPA); Canadian Numbering Administrator (CNAC); Plan national de numérotation (ARCEP)