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:
QueryStringborrows 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.QueryStringOwnedowns 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 viaQueryString::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§
- Query
String - A zero-allocation query string builder for percent encoding key-value pairs.
- Query
String Owned - An owning query string builder for percent encoding key-value pairs.
Enums§
- Part
- A borrowed key or value of a query string pair.