Match Status Codes Into Retry Classes
Dispatch HTTP status codes through match range and or-patterns, mapping each class to one retry decision with the client-error arm ordered after its exceptions.
Code
/// 把 HTTP 状态码映射成重试决策:范围模式与或模式并用
fn retry_class(status: u16) -> &'static str {
match status {
200..=299 => "no retry: request succeeded",
408 | 429 => "retry with backoff: rate or timeout",
500..=599 => "retry: server-side failure",
// 例外分支必须排在 400..=499 之前,否则先被宽范围截走
400..=499 => "no retry: request itself is bad",
_ => "unknown: treat as failure",
}
}
fn main() {
for status in [200, 204, 408, 429, 404, 503, 418] {
println!("{status} -> {}", retry_class(status));
}
}Tracing the logic
The code’s whole job belongs to the functions retry_class and main.
One loop does the cycling here: for status in [200, 204, 408, 429, 404, 503, 418]. A match spells out every case; omit one and the code does not compile.
What to watch for
- Toolchain on its own is enough — no dependency resolution ever happens.
Applying it
The entry-point signature is fn retry_class(status: u16) -> &'static str
Launch the file; main takes over and steps through retry_class.
Statements that matter
match status {
200..=299 => "no retry: request succeeded",
408 | 429 => "retry with backoff: rate or timeout",
500..=599 => "retry: server-side failure",
// 例外分支必须排在 400..=499 之前,否则先被宽范围截走
400..=499 => "no retry: request itself is bad",
_ => "unknown: treat as failure",
}
Interactive Tools for This Topic
HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.HTTP Status Code ExplorerBrowse and explore HTTP status codes with their meanings, categories, and common use cases. Search by code or description to understand 1xx through 5xx responses.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.
FAQ
- How do you run Match Status Codes Into Retry Classes?
- Run it as a complete program — compile or interpret the file directly; there is nothing to install first.
Report a problem with this exampleOpens a pre-filled issue on GitHub.