pub struct QueryString<'a> { /* private fields */ }Expand description
A zero-allocation query string builder for percent encoding key-value pairs.
This builder borrows all keys and values. Building performs a single Vec
allocation for the pair list; rendering percent-encodes each value on the
fly without allocating intermediate strings.
If you need a builder without a lifetime parameter — e.g. to store it in a
struct or return it from a function that owns the values — use
QueryStringOwned or convert via into_owned.
§Example
use query_string_builder::QueryString;
let tasty = true;
let qs = QueryString::new()
.with("q", "apple")
.with("tasty", &tasty)
.with_opt("category", Some("fruits and vegetables"));
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=apple&tasty=true&category=fruits%20and%20vegetables"
);§Borrowing footgun
Because the builder borrows its values, temporaries created inline do not live long enough — bind them to a variable first:
use query_string_builder::QueryString;
let qs = QueryString::new().with("answer", &42.to_string()); // temporary dropped here
println!("{qs}");use query_string_builder::QueryString;
let answer = 42.to_string();
let qs = QueryString::new().with("answer", &answer);
assert_eq!(qs.to_string(), "?answer=42");Implementations§
Source§impl<'a> QueryString<'a>
impl<'a> QueryString<'a>
Sourcepub fn with<K, V>(self, key: K, value: V) -> Self
pub fn with<K, V>(self, key: K, value: V) -> Self
Appends a key-value pair to the query string.
§Example
use query_string_builder::QueryString;
let answer = 42;
let qs = QueryString::new()
.with("q", "🍎 apple")
.with("category", "fruits and vegetables")
.with("answer", &answer);
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&answer=42"
);Sourcepub fn with_opt<K, V>(self, key: K, value: Option<V>) -> Self
pub fn with_opt<K, V>(self, key: K, value: Option<V>) -> Self
Appends a key-value pair to the query string if the value exists.
§Example
use query_string_builder::QueryString;
let works = true;
let qs = QueryString::new()
.with_opt("q", Some("🍎 apple"))
.with_opt("f", None::<&str>)
.with_opt("category", Some("fruits and vegetables"))
.with_opt("works", Some(&works));
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&works=true"
);Sourcepub fn push<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn push<K, V>(&mut self, key: K, value: V) -> &mut Self
Appends a key-value pair to the query string.
§Example
use query_string_builder::QueryString;
let mut qs = QueryString::new();
qs.push("q", "apple");
qs.push("category", "fruits and vegetables");
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=apple&category=fruits%20and%20vegetables"
);Sourcepub fn push_opt<K, V>(&mut self, key: K, value: Option<V>) -> &mut Self
pub fn push_opt<K, V>(&mut self, key: K, value: Option<V>) -> &mut Self
Appends a key-value pair to the query string if the value exists.
§Example
use query_string_builder::QueryString;
let mut qs = QueryString::new();
qs.push_opt("q", None::<&str>);
qs.push_opt("q", Some("🍎 apple"));
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=%F0%9F%8D%8E%20apple"
);Sourcepub fn append(&mut self, other: QueryString<'a>)
pub fn append(&mut self, other: QueryString<'a>)
Appends another query string builder’s values.
§Example
use query_string_builder::QueryString;
let mut qs = QueryString::new().with("q", "apple");
let more = QueryString::new().with("q", "pear");
qs.append(more);
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=apple&q=pear"
);Sourcepub fn append_into(self, other: QueryString<'a>) -> Self
pub fn append_into(self, other: QueryString<'a>) -> Self
Appends another query string builder’s values, consuming both types.
§Example
use query_string_builder::QueryString;
let qs = QueryString::new().with("q", "apple");
let more = QueryString::new().with("q", "pear");
let qs = qs.append_into(more);
assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=apple&q=pear"
);Sourcepub fn into_owned(self) -> QueryStringOwned
pub fn into_owned(self) -> QueryStringOwned
Converts this borrowing builder into a QueryStringOwned by rendering
each key and value to an owned String.
Useful for building cheaply with borrows and then storing or returning the result past the borrows’ lifetimes.
§Example
use query_string_builder::{QueryString, QueryStringOwned};
let qs: QueryStringOwned = {
let q = String::from("apple");
QueryString::new().with("q", &q).into_owned()
};
assert_eq!(qs.to_string(), "?q=apple");Trait Implementations§
Source§impl<'a> Clone for QueryString<'a>
impl<'a> Clone for QueryString<'a>
Source§fn clone(&self) -> QueryString<'a>
fn clone(&self) -> QueryString<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more