str/str.rs
1extern crate pct_str;
2
3use pct_str::{InvalidPctString, PctStr};
4
5fn main() -> Result<(), InvalidPctString<&'static str>> {
6 // [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
7 let buffer = "Hello%20World%21";
8 // It is just a reference to `buffer`.
9 // It can fail if `buffer` is not a valid percent-encoded string.
10 let pct_str = PctStr::new(buffer)?;
11
12 // You can compare percent-encoded strings with a regular string.
13 assert!(pct_str == "Hello World!"); // => true
14
15 // The underlying string is unchanged.
16 assert!(pct_str.as_str() == "Hello%20World%21"); // => true
17
18 // Just as a regular string, you can iterate over the
19 // encoded characters of `pct_str` with [`PctStr::chars`].
20 for c in pct_str.chars() {
21 print!("{}", c);
22 }
23 // => Hello World!
24
25 println!("");
26
27 // You can decode the string and every remove percent-encoded characters
28 // with the [`PctStr::decode`] method.
29 let decoded_string: String = pct_str.decode();
30 println!("{}", decoded_string);
31 // => Hello World!
32
33 Ok(())
34}