Skip to main content

percent_decode

Function percent_decode 

Source
pub fn percent_decode(s: &str) -> Cow<'_, str>
Expand description

Percent-decode a string.

Returns a Cow::Borrowed if no decoding was needed (most common case), or Cow::Owned if percent sequences were decoded.

Handles:

  • Standard percent-encoding (%XX)
  • UTF-8 multi-byte sequences
  • Plus sign as space (common in form data)

Invalid sequences are left as-is for robustness.

ยงExample

use fastapi_http::percent_decode;

// No decoding needed - returns borrowed
let simple = percent_decode("hello");
assert!(matches!(simple, std::borrow::Cow::Borrowed(_)));

// Decoding needed - returns owned
let encoded = percent_decode("hello%20world");
assert_eq!(&*encoded, "hello world");

// Plus as space
let plus = percent_decode("hello+world");
assert_eq!(&*plus, "hello world");