Parsing phone numbers with Rust and R

[This article was first published on schochastics - all things R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

On my bucket list for 2024 is to learn a bit of Rust. After reading (well skimming…) THE BOOK and doing a few experiments, I wanted to give it a try to incorporate some Rust code in an R package. I chose to wrap the phonenumber crate to parse phone numbers into R, because I thought thats small enough of a task that I could manage. Note that there is already an R package for that, called dialr, which wraps Google’s libphonenumber Java library. For obvious reasons, my experimental package is called dialrs.

As a small disclaimer, I am far from being an expert with Rust, so don’t take this package of an example of best practices in (wrapped) Rust.

Setup

Setting up a package for Rust code is extremely simple. All you need is the rextendr package which allows to call Rust code from R. A vignette explains how to use it within an R package.

Two commands are enough to get you started with a package.

usethis::create_package("dialrs")
rextendr::use_extendr()

The function use_extendr() creates a whole bunch of files and directories (see the vignette for help). The most important ones are src/rust/Cargo.toml to include crates as dependencies and src/rust/src/lib.rs where most of the Rust code will live. You can also have the code in different files, say phone.rs, but then there needs to be a line

mod phone

in libs.rs.
To compile the code, we need to run rextendr::document() (similar to devtools::document())

The rest of the f**** packages

Implementing the actual package really felt like that old owl meme. Sure, the setup is incredibly easy, but you still need to write the Rust code yourself. Surprisingly. There was lot of trial and error but eventually, I got the following stack of functions to work properly.

library(dialrs)
numbers <- c("+1 202-555-0173", "+33 122 334 455", "+1 1 800 FLOWERS")
phone_is_valid(numbers)
[1]  TRUE  TRUE FALSE
phone_type(numbers)
[1] "fixed_line_or_mobile" "fixed_line"           "unknown"             
phone_country_code(numbers)
[1]  1 33  1
phone_parse(numbers, format = "International")
[1] "+1 202-555-0173"   "+33 1 22 33 44 55" "+1 18003569377"   
phone_parse(numbers, format = "National")
[1] "(202) 555-0173" "01 22 33 44 55" "18003569377"   
phone_parse(numbers, format = "RFC3966")
[1] "tel:+1-202-555-0173"   "tel:+33-1-22-33-44-55" "tel:+1-18003569377"   
phone_parse(numbers, format = "E.164")
[1] "+12025550173"  "+33122334455"  "+118003569377"

Feel free to check out my libs.rs for pointers to use Rust with R and of course to correct my horrible style.

Reuse

Citation

BibTeX citation:
@online{schoch2024,
  author = {Schoch, David},
  title = {Parsing Phone Numbers with {Rust} and {R}},
  date = {2024-02-23},
  url = {http://blog.schochastics.net/posts/2024-02-23_parsing-phone-numbers-with-rust-and-r},
  langid = {en}
}
For attribution, please cite this work as:
Schoch, David. 2024. “Parsing Phone Numbers with Rust and R.” February 23, 2024. http://blog.schochastics.net/posts/2024-02-23_parsing-phone-numbers-with-rust-and-r.
To leave a comment for the author, please follow the link and comment on their blog: schochastics - all things R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)