Skip to content

build_query

Builds a query string (a=1&b=two) from key-value pairs, percent-encoding both sides.

Pairs keep their order and repeated keys are kept. A space becomes %20 (not +), which every URL parser reads back correctly. The result has no leading ?. It is the inverse of parse_query.

use helpers4::url::build_query;

Cargo feature url (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features url

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["url"] }
pub fn build_query<I, K, V>(pairs: I) -> String
where
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<str>,
    V: AsRef<str>,
ParameterTypeDescription
pairsIThe keys and values, anything that iterates over (key, value) of string-likes.

String — The encoded query string, empty when there are no pairs.

use helpers4::url::build_query;

assert_eq!(build_query([("q", "rust lang"), ("page", "2")]), "q=rust%20lang&page=2");
assert_eq!(build_query(Vec::<(&str, &str)>::new()), "");

src/url/build_query.rs