URLs (std::url)
std::url provides pure utilities for working with web addresses: decomposition
into parts, percent-encoding and decoding, and query string assembly. Because all
operations are local computations (no network or filesystem), the examples run
directly in the sandbox.
URL parsing
url.parse breaks an address into protocol, host, port, path, query,
fragment and a params map with the parameters already decoded.
Extracts each component of a full URL, including params.
// Feature: url.parse — break a URL into protocol, host, port, path, query, fragment
// When to use: extract parts of an address for routing, validation, or logging.
use std::url
let parts = url.parse("https://api.example.com:8443/users/42?id=42&active=true#bio")
print(parts["protocol"]) // expected: https
print(parts["host"]) // expected: api.example.com
print(parts["port"]) // expected: 8443
print(parts["path"]) // expected: /users/42
print(parts["query"]) // expected: id=42&active=true
print(parts["fragment"]) // expected: bio
// Query params are already parsed into params.
print(parts["params"]["id"]) // expected: 42
print(parts["params"]["active"]) // expected: true
Percent-encoding and decoding
url.encode / url.decode handle spaces and special characters; the
encode_component / decode_component variant is more aggressive and also
escapes /, ? and # — ideal for values inside a query string.
Round-trip between a raw string and its percent-encoded representation.
// Feature: url.encode / url.decode — percent-encoding
// When to use: pass strings with spaces or special characters in URLs.
use std::url
// encode applies percent-encoding to non-safe characters.
let raw = "hello world & friends"
let enc = url.encode(raw)
print(enc) // expected: hello%20world%20%26%20friends
// decode reverses the operation.
print(url.decode(enc)) // expected: hello world & friends
// encode_component is more aggressive — it also escapes /, ?, #.
let comp = url.encode_component("a/b?c=d")
print(comp) // expected: a%2Fb%3Fc%3Dd
let back = url.decode_component(comp)
print(back) // expected: a/b?c=d
Building query strings
Combine url.encode_component with concatenation and Array.join to build
query strings dynamically without relying on external libraries.
Parsing existing params and manually building an encoded query string.
// Feature: url — build and parse query strings
// When to use: HTTP clients, links with filters, pagination.
use std::Array
use std::url
// Parse: query string already comes in `params` when using url.parse.
let parts = url.parse("https://api.example.com/search?q=zolo+lang&page=2&limit=20")
print(parts["params"]["q"]) // expected: zolo+lang
print(parts["params"]["page"]) // expected: 2
print(parts["params"]["limit"]) // expected: 20
// Manual construction: join "k=v" pairs with "&".
fn build_query(pairs: [str]) -> str {
return pairs.join("&")
}
let qs = build_query([
"q=" + url.encode_component("hello world"),
"page=1",
"active=true",
])
print(qs) // expected: q=hello%20world&page=1&active=true
Challenge
Given a map #{ q: "zolo lang", page: 1 }, write a to_query_string function
that iterates the keys and produces q=zolo%20lang&page=1.
See also