rust_icu: low-level rust language bindings for the ICU library
|ICU 63, 68..70|
This is a library of low level native rust language bindings for the International Components for Unicode (ICU) library for C (a.k.a. ICU4C).
If you just want quick instructions on how to download and install, see the quickstart guide
The latest version of this file is available at https://github.com/google/rust_icu.
This is not an officially supported Google product.
Why wrap ICU (vs. doing anything else)?
The rust language Internationalisation page confirms that ICU support in rust is spotty, so having a functional wrapper helps advance the state of the art.
Projects such as Fuchsia already depend on ICU, and having rust bindings allows for an easy way to use Unicode algorithms without taking on more dependencies.
Cooperation on the interface with projects such as the ICU4X could allow seamless transition to an all-rust implementation in the future.
Structure of the repository
The repository is organized as a cargo workspace of rust crates. Each crate corresponds to the respective header in the ICU4C library's C API. Please consult the coverage report for details about function coverage in the headers.
|rust_icu||Top-level crate. Include this if you just want to have all the functionality available for use.|
|rust_icu_common||Commonly used low-level wrappings of the bindings.|
|rust_icu_intl||Implements ECMA 402 recommendation APIs.|
|rust_icu_sys||Low-level bindings code|
|rust_icu_ubrk||Support for text boundary analysis. Implements
|rust_icu_ucal||ICU Calendar. Implements
|rust_icu_ucol||Collation support. Implements
|rust_icu_udat||ICU date and time. Implements
|rust_icu_udata||ICU binary data. Implements
|rust_icu_uenum||ICU enumerations. Implements
|rust_icu_uformattable||Locale-sensitive list formatting support. Implements
|rust_icu_ulistformatter||Locale-sensitive list formatting support. Implements
|rust_icu_uloc||Locale support. Implements
|rust_icu_umsg||MessageFormat support. Implements
|rust_icu_unorm2||Unicode normalization support. Implements
|rust_icu_unum||Number formatting support. Implements
|rust_icu_unumberformatter||Number formatting support (modern). Implements
|rust_icu_upluralrules||Locale-sensitive plural rules support. Implements
|rust_icu_ustring||ICU strings. Implements
|rust_icu_utext||Text operations. Implements
|rust_icu_utrans||Transliteration support. Implements
The generated rust language binding methods of today limit the availability of language bindings to the available C API. The ICU library's C API (sometimes referred to as ICU4C in the documentation) is distinct from the ICU C++ API.
The bindings offered by this library have somewhat limited applicability, which means it may sometimes not work for you out of the box. If you come across such a case, feel free to file a bug for us to fix. Pull requests are welcome.
The limitations we know of today are as follows:
There isn't a guaranted feature parity. Some algorithms that are implemented in C++ don't have a C equivalent, and vice-versa. This is usually not a problem if you are using the library from C++, since you are free to choose whichever API surface works for you. But it is an issue for rust bindings, since we can only use the C API at the moment.
A C++ implementation of a new algorithm is not necessarily always reflected in the C API, leading to feature disparity between the C and C++ API surfaces. See for example this bug as an illustration.
icu_configfeature will likely allow you some freedom to auto-generate bindings for your own library version, we still need to keep a list of explicitly supported ICU versions to ensure that the wrappers are stable.
The compatibility guarantee is as follows:
- Automated tests are executed for last three major ICU library versions in all feature combinations of interest.
- Automated tests are executed for the ICU library version in use by the docs.rs system (so the documentation could be built).
||ICU 63.x||ICU 67.1||ICU 68.1||ICU 69.1||ICU 70.1||ICU 71.1|
If you must use a 0.x relase, please use the release 0.5. Otherwise, we recommend using the newest release that supports your use case.
rust_icu library is intended to be compiled with
cargo, with one of
several features enabled. Compilation with
cargo allows us to do some library
detection in a custom
build.rs file in the
rust_icu_sys library and adapt
the build process to your build environment. However, since not every
development environment will use the same settings, we opted to offer certain
features (below) as configuration options.
While our intention is to keep the list of features below up to date with the
actual list in
the list may periodically go out of date.
To use any of the features, you will need to activate the feature in all the
rust_icu_* crates that you intend to use. Failing to do this will result in
confusing compilation end result.
||Yes||If set, cargo will run
||Yes||If set, ICU bindings are generated with version numbers appended. This is called "renaming" in ICU, and is normally needed only when linking against specific ICU version is required, for example to work around having to link different ICU versions. See the ICU documentation for a discussion of renaming. This feature MUST be used when
||Yes||If set, the binary icu-config will be used to configure the library. Turn this feature off if you do not want
||No||If set, ICU bindings are made for the ICU version specified in the environment variable
git clone https://github.com/google/rust_icu.git
Install from https://rustup.rs. Used to set toolchain defaults. This will install
You must have Clang installed to access the right headers.
The ICU library development environmnet
You will need access to the ICU libraries for the
rust_icubindings to link against. Download and installation of ICU is out of scope of this document. Please read through the ICU introduction to learn how to build and install.
Sometimes, the ICU library will be preinstalled on your system, or you can pull the library in from your package management program. However, this library won't necessarily be the one that you need to link into the program you are developing. In short, it is your responsibility to have a developer version of ICU handy somewhere on your system.
We have a quickstart install that may get you well on the way in case your environment happens to be configured very similarly to ours and you want to build ICU from source.
GNU Make, if you want to use the make-based build and test.
Installing GNU Make is beyond the scope of this file. Please refer to your OS instructions for installation.
docker, if you decide to use docker-based build and test.
dockeris beyond the scope of this file, please see the docker installation instructions for details. As installing
dockeris intrusive to the host machine, your company may have internal documentation on how to install
icu_configfeature is used.
You need to install the ICU library on your system, such that the binary
icu-configis somewhere in your
$PATH. The build script will use it to discover the library settings and generate correct link scripts. If you use the feature but
icu-configis not found,
bindgenfeature is used.
bindgen user guide for instructions on how to install it.
bindgenfeature is used.
See https://github.com/rust-lang/rustfmt for instructions on how to install.
There are a few options to run the test for
Building and testing using
cargo is the canonical way of building and testing
In the case of the
rust_icu library you may find that your system's default
ICU development package is ancient, in which case you will need to build your
own ICU4C library (see below for that). That will make it necessary to pass in
LD_LIBRARY_PATH environment variables to help the bulid
code locate and use the library you built, instead of the system default.
The following tests should all build and pass. Note that because the libraries
needed are in a custom location, we need to set
LD_LIBRARY_PATH when running
the tests, as well as
If you find that you are able to use your system's default ICU installation, you can safely omit the two libraries.
env PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig" \ LD_LIBRARY_PATH="$HOME/local/lib" \ bash -c 'cargo test'
If you think that the above approach is too much of a hassle, consider trying out the Docker-based approach.
If you happen to like the GNU way of doing things, you may appreciate the GNU Make approach.
The easiest way is to use GNU Make and run:
You may want to use this method if you are working on
rust_icu, have your
development environment all set up and would like a shorthand to run the tests.
See optional dependencies section above.
To run a hermetic build and test of the
rust_icu source code, issue the
This will run docker-based build and test of the source code on your local machine. This is a good way to test that your code works with a specific reference version of ICU.
There is plenty of prior art that has been considered:
The current state of things is that I'd like to do a few experiments on my own first, then see if the work can be folded into any of the above efforts.
There are a few competing approaches for ICU bindings. However, it seems, at least based on information available in rust's RFC repos, that the work on ICU support in rust is still ongoing.
These are the assumptions made in the making of this library:
We need a complete, reusable and painless ICU low-level library for rust.
This, for example, means that we must rely on an external ICU library, and not lug the library itself with the binding code. Such modularity allows the end user of the library to use an ICU library of their choice, and incorporate it in their respective systems.
No ICU algorithms will be reimplemented as part of the work on this library.
An ICU reimplementation will likely take thousands of engineer years to complete. For an API that is as subtle and complex as ICU, I think that it is probably a better return on investment to maintain a single central implementation.
Also, the existence of this library doesn't prevent reimplementation. If someone else wants to try their hand at reimplementing ICU, that's fine too.
This library should serve as a low-level basis for a rust implementation.
A low level ICU API may not be an appropriate seam for the end users. A rust-ful API should be layered on top of these bindings. It will probably be a good idea to subdivide that functionality into crates, to match the expectations of rust developers.
I'll gladly reuse the logical subdivision already made in some of the above mentioned projects.
I'd like to explore ways to combine with existing implementations to build a complete ICU support for rust.
Hopefully it will be possible to combine the good parts of all the rust bindings available today into a unified rust library. I am always available to discuss options.
The only reason I started a separate effort instead of contributing to any of the projects listed in the "Prior Art" section is that I wanted to try what a generated library would look like in rust.
Before you begin, please ensure the following prerequisites are met:
- You have docker installed and it runs on your system.
- You have GNU Make.
- You have git.
- You have plenty of disk space. The docker images for the build environment are a bit large, so a few GiB are needed to fit all of them.
- You have an Internet connection.
From there, the following sequence of commands will check out, build and test
rust_icu source code.
mkdir -p ~/tmp cd tmp git clone https://github.com/google/rust_icu cd rust_icu make docker-test
You can now make changes to the code and tests. You can re-run the compile and
test cycle by running
ICU installation instructions
These instructions follow the "out-of-tree" build instructions from the ICU repository.
The instructions below are not self-contained. They assume that:
- you have your system set up such that you can follow the ICU build instructions effectively. This requires some upfront time investment.
- you can build ICU from source, and your project has access to ICU source.
- your setup is Linux, with some very specific settings that worked for me. You may be able to adapt them to work on yours.
mkdir -p $HOME/local mkdir -p $HOME/tmp cd $HOME/tmp git clone https://github.com/unicode-org/icu.git mkdir icu4c-build cd icu4c-build ../icu/icu4c/source/runConfigureICU Linux \ --prefix=$HOME/local \ --enable-static make make install make doc
If the compilation finishes with success, the directory
have the file
icu-config which is necessary to discover the library
You can also do a
to run the unit tests.
If you add
$PATH, or move
icu-config to a directory
that is listed in your
$PATH you should be all set to compile
ICU rebuilding instructions
If you change the configuration of the ICU library with an intention to rebuild
the library from source you should probably add an intervening
Since the ICU build is not hermetic, this ensures there are no remnants of the old compilation process sitting around in the build directory. You need to do this for example if you upgrade the major version of the ICU library. If you forget to do so, you may see unexpected errors while compiling ICU, or while linking or running your programs.
Compiling for a set version of ICU
- You have selected the feature set
- You have manually verified that the compatibility matrix has a "Yes" for the ICU version and feature set you want to use.
The following is a tested example.
env PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig" \ LD_LIBRARY_PATH="$HOME/local/lib" \ RUST_ICU_MAJOR_VERSION_NUMBER=65 \ bash -c 'cargo test'
The following would be an as of yet untested example of compiling
against a preexisting ICU version 66.
env PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig" \ LD_LIBRARY_PATH="$HOME/local/lib" \ RUST_ICU_MAJOR_VERSION_NUMBER=66 \ bash -c 'cargo test'
Adding support for a new version of ICU.
In general, as long as
icu-config approach is supported, it should be possible
to generate the library wrappers for newer versions of the ICU library, assuming
that the underlying C APIs do not diverge too much.
An approach that yielded easy support for ICU 65.1 consisted of the following
$RUST_ICU_SOURCE_DIR is the directory where you extracted the
ICU source code.
- Download the new ICU version from source to
- Build the ICU library following for example the compilation steps above with the new version.
- Get the file
lib.rsfrom the output directory
$RUST_ICU_SOURCE_DIR/target/debug/build/rust_icu_sys-..., rename it to
lib_66.rs(if working with ICU version 66, otherwise append the version you are using).
- Save the file to the directory
$RUST_ICU_SOURCE_DIR/rust_icu_sys/bindgen, this is the directory that contains the pre-generated sources.
lib_XX.rs may need to be generated again if
build.rs is changed
to include more features.
Adding more bindings
When adding more ICU wrappers, make sure to do the following:
rust_icu_sys/bindgen/run_bindgen.shto add appropriate lines into
Testing with a specific feature set turned on
Here's an example of running a docker test on ICU 67, with features
renaming turned on instead of the default. Note that
the parameters are mostly passed into the container that runs
make DOCKER_TEST_ENV=rust_icu_testenv-67 \ RUST_ICU_MAJOR_VERSION_NUMBER=67 \ DOCKER_TEST_CARGO_TEST_ARGS='--no-default-features --features icu_version_in_env,renaming' \ docker-test
- The environment variable
RUST_ICU_MAJOR_VERSION_NUMBERis used for the feature
cargoto use the file
rust_icu_sys/bindgen/lib_67.rsas a prebuilt bindgen source file instead of trying to generate one on the fly.
- The environment variable
DOCKER_TEST_CARGO_TEST_ARGSis used to pass the command line arguments to the
cargo testwhich is used in the docker container. The environment is passed in verbatim to
cargo testwithout quoting, so separate words in the environment end up being separate args to
- The environment variable
DOCKER_TEST_ENVis the base name of the Docker container used to run the test in. The container
rust_icu_testenv-67is a container image that contains preinstalled environment with a compiled version of ICU 67.
Refreshing static bindgen files
make static-bindgen periodically, to refresh the statically generated
bindgen files (named
XX is an ICU version, e.g. 67) in the
rust_icu_sys/bindgen which are used when
bindgen features are turned off.
Invoking this make target will modify the local checkout with the newer versions
of the files
lib_XX.rs. Make a pull request and check them in.
For more information on why this is needed, see the bindgen README.md.