Skip to main content

string/
string.rs

1extern crate pct_str;
2
3use pct_str::{InvalidPctString, PctStr, PctString};
4
5fn main() -> Result<(), InvalidPctString<String>> {
6	// [`PctString`] is the equivalent of [`String`] for
7	// percent-encoded strings.
8	// The data is owned by `pct_string`.
9	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;
10
11	// You can compare percent-encoded strings with a regular string.
12	assert!(pct_string == "Hello World!");
13
14	// The underlying string is percent-encoded.
15	assert!(pct_string.as_str() == "Hello%20World%21");
16
17	// You can get a reference to the string as a [`PctStr`].
18	assert!(
19		pct_string.as_pct_str()
20			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
21	);
22
23	// Just as a regular string, you can iterate over the
24	// encoded characters of `pct_str` with [`PctString::chars`].
25	for c in pct_string.chars() {
26		println!("{}", c);
27	}
28
29	// You can decode the string and every remove percent-encoded characters
30	// with the [`PctStr::decode`] method.
31	let decoded_string: String = pct_string.decode();
32	println!("{}", decoded_string);
33
34	Ok(())
35}