DevToolsForYou

URL Encoder and Decoder in Rust — Code Examples

URL Encoder and Decoder in RustUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

Rust has the urlencoding crate for simple percent-encoding, and the url crate for full URL building and parsing.

Rust
// Cargo.toml: urlencoding = "2"
use urlencoding::{encode, decode};

fn main() {
    let input = "hello world & foo=bar";

    // Encode (RFC 3986 percent-encoding, spaces as %20)
    let encoded = encode(input);
    println!("{}", encoded); // hello%20world%20%26%20foo%3Dbar

    // Decode
    let decoded = decode("hello%20world%20%26%20foo%3Dbar").unwrap();
    println!("{}", decoded); // hello world & foo=bar
}

// Build a full URL with query params using the url crate:
// Cargo.toml: url = "2"
use url::Url;

fn main() {
    let mut url = Url::parse("https://example.com/search").unwrap();
    url.query_pairs_mut()
        .append_pair("q", "hello world & foo=bar")
        .append_pair("page", "1");
    println!("{}", url);
    // https://example.com/search?q=hello+world+%26+foo%3Dbar&page=1
}
Notes & gotchas
  • urlencoding::encode follows RFC 3986 and encodes spaces as %20; the url crate encodes query pairs with spaces as +.
  • Use the url crate whenever you need to parse or construct full URLs — it handles all edge cases correctly.
  • decode() returns a Cow<str> — it borrows if no decoding is needed, allocating only when necessary.
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages