#iri #uri #resource #identifier #slice


Internationalized Resource Identifiers (IRIs) and References, borrowed and owned

19 stable releases

2.2.3 Jan 11, 2023
2.2.2 Dec 20, 2022
2.2.0 Aug 4, 2022
2.1.3 Apr 16, 2022
1.1.3 Mar 31, 2020

#23 in Web programming

Download history 6559/week @ 2023-02-08 7215/week @ 2023-02-15 6861/week @ 2023-02-22 6245/week @ 2023-03-01 7731/week @ 2023-03-08 7877/week @ 2023-03-15 8054/week @ 2023-03-22 8053/week @ 2023-03-29 6337/week @ 2023-04-05 5353/week @ 2023-04-12 5998/week @ 2023-04-19 5518/week @ 2023-04-26 5784/week @ 2023-05-03 5752/week @ 2023-05-10 5981/week @ 2023-05-17 4155/week @ 2023-05-24

22,650 downloads per month
Used in 44 crates (27 directly)


4.5K SLoC

Internationalized Resource Identifiers and References

CI Crate informations License Documentation

This crates gives an implementation of Internationalized Resource Identifiers (IRIs) and IRI references following RFC 3987 and RFC 3986 defined by the Internet Engineering Task Force (IETF). IRIs are a superclass of Uniform Resource Identifier (URIs) and Uniform Resource Locator (URLs) used to uniquely identify objects across the web. An IRI is defined as a sequence of characters with distinguishable components: a scheme, an authority, a path, a query and a fragment.

    \_/   \______________/\_________/ \_________/ \__/
     |           |            |            |        |
  scheme     authority       path        query   fragment

This crate provides the four types Iri, IriBuf, IriRef and IriRefBuf to manipulate byte/string slices and buffers as IRIs and IRI references. Theses allows the easy access and manipulation of every components. It features:

  • borrowed and owned IRIs and IRI-reference;
  • mutable IRI buffers (in-place);
  • path normalization;
  • comparison modulo normalization;
  • IRI-reference resolution;
  • static IRI parsing with the static-iref crate and its iri macro; and
  • serde support (by enabling the serde feature).

Basic usage

You can parse IRI strings by wrapping an Iri instance around a str slice. Note that no memory allocation occurs using Iri, it only borrows the input data. Access to each component is done in constant time.

extern crate iref;

use iref::Iri;

let iri = Iri::new("https://www.rust-lang.org/foo/bar?query#frag")?;

println!("scheme: {}", iri.scheme());
println!("authority: {}", iri.authority().unwrap());
println!("path: {}", iri.path());
println!("query: {}", iri.query().unwrap());
println!("fragment: {}", iri.fragment().unwrap());

IRIs can be created and modified using the IriBuf type. With this type, the IRI is held in a single buffer, modified in-place to reduce memory allocation and optimize memory accesses. This also allows the conversion from IriBuf into Iri.

extern crate iref;

use std::convert::TryInto;
use iref::IriBuf;

let mut iri = IriBuf::new("https://www.rust-lang.org")?;


assert_eq!(iri, "https://www.rust-lang.org:40/foo/bar?query#fragment");

The try_into method is used to ensure that each string is syntactically correct with regard to its corresponding component (for instance, it is not possible to replace "query" with "query?" since ? is not a valid query character).

Detailed Usage

Path manipulation

The IRI path is accessed through the path or path_mut methods. It is possible to access the segments of a path using the iterator returned by the segments method.

for segment in iri.path().segments() {
	println!("{}", segment);

One can use the normalized_segments method to iterate over the normalized version of the path where dot segments (. and ..) are removed. In addition, it is possible to push or pop segments to a path using the corresponding methods:

let mut iri = IriBuf::new("https://rust-lang.org/a/c")?;
let mut path = iri.path_mut();

path.push("c/".try_into()?); // a `/` character is allowed at the end of a segment.

assert_eq!(iri.path(), "/a/b/c/");

IRI references

This crate provides the two types IriRef and IriRefBuf to represent IRI references. An IRI reference is either an IRI or a relative IRI. Contrarily to regular IRIs, relative IRI references may have no scheme.

let mut iri_ref = IriRefBuf::default(); // an IRI reference can be empty.

// An IRI reference with a scheme is a valid IRI.
let iri: Iri = iri_ref.as_iri()?;

// An IRI can be safely converted into an IRI reference.
let iri_ref: IriRef = iri.into();

Given a base IRI, references can be resolved into a regular IRI using the Reference Resolution Algorithm defined in RFC 3986. This crate provides a strict implementation of this algorithm.

let base_iri = Iri::new("http://a/b/c/d;p?q")?;
let mut iri_ref = IriRefBuf::new("g;x=1/../y")?;

// non mutating resolution.
assert_eq!(iri_ref.resolved(base_iri), "http://a/b/c/y");

// in-place resolution.
assert_eq!(iri_ref, "http://a/b/c/y");

This crate implements Errata 4547 about the abnormal use of dot segments in relative paths. This means that for instance, the path a/b/../../../ is normalized into ../.

IRI comparison

Here are the features of the IRI comparison method implemented in this crate.

Protocol agnostic

This implementation does not know anything about existing protocols. For instance, even if the HTTP protocol defines 80 as the default port, the two IRIs http://example.org and http://example.org:80 are not equivalent.

Every / counts

The path /foo/bar is not equivalent to /foo/bar/.

Path normalization

Paths are normalized during comparison by removing dot segments (. and ..). This means for instance that the paths a/b/c and a/../a/./b/../b/c are equivalent. Note however that this crate implements Errata 4547 about the abnormal use of dot segments in relative paths. This means that for instance, the IRI http:a/b/../../../ is equivalent to http:../ and not http:.

Percent-encoded characters

Thanks to the pct-str crate, percent encoded characters are correctly handled. The two IRIs http://example.org and http://exa%6dple.org are equivalent.

What is missing

For now, this crate lacks of a proper way to compare strings in a case insensitive manner. As a result, the IRIs http://example.org and htTp://ExAmpLe.Org that should be equivalent are not.

What's next?

I am waiting for Custom DSTs to be available in order to turn the types Iri and IriRef (and Path, Authority, etc.) into dynamically sized types just like str, OsStr, etc. This would allow buffered types (IriBuf, PathBuf, etc.) to Deref into those DSTs and be far more easy to use while simplifying the API.

We just need to wait...


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.