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.

RustResilience

Code

snippet
/// 把 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

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.