rust
33 lines · 7 steps
Parsing query strings in Axum handlers
How Axum's Query extractor deserializes URL parameters into a typed struct with sensible defaults.
Explained by
highlit
1use axum::{
2 extract::Query,
3 response::IntoResponse,
4 Json,
5};
6use serde::Deserialize;
7
8#[derive(Debug, Deserialize)]
9pub struct ArticleFilter {
10 #[serde(default)]
11 tag: Vec<String>,
12 #[serde(default)]
13 author: Option<String>,
14 #[serde(default = "default_limit")]
15 limit: u32,
16}
17
18fn default_limit() -> u32 {
19 20
20}
21
22pub async fn list_articles(
23 Query(filter): Query<ArticleFilter>,
24) -> impl IntoResponse {
25 let articles = Article::query()
26 .filter_tags(&filter.tag)
27 .filter_author(filter.author.as_deref())
28 .limit(filter.limit)
29 .fetch_all()
30 .await;
31
32 Json(articles)
33}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1The Query extractor turns raw URL parameters into a validated, typed struct before your handler runs.
- 2serde's default attribute lets each field fall back gracefully when a parameter is absent from the request.
- 3Building the response from a typed filter keeps handlers thin and pushes query logic into the query builder.
Related explainers
rust
use rand::distributions::{Alphanumeric, DistString}; use rand::rngs::OsRng; #[derive(Debug, Clone, PartialEq, Eq)]
A newtype wrapper for API tokens in Rust
newtype-pattern
randomness
encapsulation
Intermediate
6 steps
rust
pub fn normalize_path(input: &str) -> String { let is_absolute = input.starts_with('/'); let has_trailing_slash = input.len() > 1 && input.ends_with('/'); let mut stack: Vec<&str> = Vec::new();
Normalizing filesystem paths in Rust
string-processing
stack
path-manipulation
Intermediate
8 steps
typescript
interface UserAgentInfo { browser: { name: string; version: string }; os: { name: string; version: string }; device: 'mobile' | 'tablet' | 'desktop';
Parsing a user-agent string with ordered rules
regex
parsing
pattern-matching
Intermediate
9 steps
rust
use axum::{ body::{Body, Bytes}, extract::Request, http::StatusCode,
Logging request and response sizes in Axum
middleware
http
streaming-bodies
Advanced
8 steps
rust
use std::borrow::Cow; pub fn escape_html(input: &str) -> Cow<'_, str> { let needs_escape = input
Zero-copy HTML escaping with Cow in Rust
clone-on-write
zero-copy
string-escaping
Intermediate
6 steps
rust
use once_cell::sync::Lazy; use regex::Regex; static CREDIT_CARD: Lazy<Regex> = Lazy::new(|| {
Redacting sensitive data from logs in Rust
regex
lazy-initialization
checksum-validation
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-query-strings-in-axum-handlers-explained-rust-c1ea/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.