Skip to main content

Crate query_string_builder

Crate query_string_builder 

Source
Expand description

§A query string builder for percent encoding key-value pairs

This is a tiny helper crate for simplifying the construction of URL query strings. The initial ? question mark is automatically prepended.

Two builders are provided:

  • QueryString borrows its keys and values and never allocates strings — neither while building nor while rendering. Use it when you assemble and format the query string in one place.
  • QueryStringOwned owns its data and has no lifetime parameter. Use it when you want to store the builder in a struct or return it from a function. A borrowing builder can be converted via QueryString::into_owned.

§Example

use query_string_builder::QueryString;

let tasty = true;
let qs = QueryString::new()
    .with("q", "🍎 apple")
    .with("tasty", &tasty)
    .with_opt("color", None::<&str>)
    .with_opt("category", Some("fruits and vegetables?"));

assert_eq!(
    format!("example.com/{qs}"),
    "example.com/?q=%F0%9F%8D%8E%20apple&tasty=true&category=fruits%20and%20vegetables?"
);

Since QueryString borrows its values, inline temporaries don’t live long enough — bind them to a variable first, or use QueryStringOwned:

use query_string_builder::QueryStringOwned;

let qs = QueryStringOwned::new()
    .with("answer", 42)
    .with("works", true);

assert_eq!(format!("example.com/{qs}"), "example.com/?answer=42&works=true");

Structs§

QueryString
A zero-allocation query string builder for percent encoding key-value pairs.
QueryStringOwned
An owning query string builder for percent encoding key-value pairs.

Enums§

Part
A borrowed key or value of a query string pair.

Traits§

IntoPart
Conversion into a borrowed query string Part.