7 stable releases

Uses new Rust 2024

3.0.1 Jan 28, 2026
2.0.0 Jul 2, 2023
1.2.0 Feb 2, 2023
1.1.0 Sep 10, 2020
1.0.2 Mar 16, 2020

#265 in Encoding

Download history 23805/week @ 2026-01-23 30650/week @ 2026-01-30 42868/week @ 2026-02-06 36881/week @ 2026-02-13 33933/week @ 2026-02-20 37610/week @ 2026-02-27 79243/week @ 2026-03-06 129638/week @ 2026-03-13 92138/week @ 2026-03-20 90955/week @ 2026-03-27 108421/week @ 2026-04-03 103565/week @ 2026-04-10 95243/week @ 2026-04-17 95786/week @ 2026-04-24 74532/week @ 2026-05-01 49234/week @ 2026-05-08

331,924 downloads per month
Used in 232 crates (12 directly)

MIT/Apache

32KB
748 lines

Percent-Encoded Strings for Rust

Build Crate informations License Documentation

This crate provides two types, PctStr and PctString, similar to str and String, representing percent-encoded strings used in URL, URI, IRI, etc. You can use them to encode, decode and compare percent-encoded strings.

Basic usage

You can parse/decode percent-encoded strings by building a PctStr slice over a str slice.

use pct_str::PctStr;

let pct_str = PctStr::new("Hello%20World%21").unwrap();
assert_eq!(pct_str, "Hello World!");

let decoded_string: String = pct_str.decode();
assert_eq!(decoded_string, "Hello World!")

To create new percent-encoded strings, use the PctString to copy or encode new strings.

use pct_str::{PctString, UriReserved};

// Copy the given percent-encoded string.
let pct_string = PctString::new("Hello%20World%21").unwrap();

// Encode the given regular string.
let pct_string = PctString::encode("Hello World!".chars(), UriReserved::Any);

assert_eq!(pct_string.as_str(), "Hello%20World%21");

You can choose which character will be percent-encoded by the encode function by implementing the Encoder trait.

use pct_str::{UriReserved, PctString};

struct CustomEncoder;

impl pct_str::Encoder for CustomEncoder {
  fn encode(&self, c: char) -> bool {
    UriReserved::Any.encode(c) || c.is_uppercase()
  }
}

let pct_string = PctString::encode("Hello World!".chars(), CustomEncoder);
assert_eq!(pct_string.as_str(), "%48ello%20%57orld%21")

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~15KB