tokio-rs/tokio · error · syn::Error

Failed to parse value of `{field}` as integer: {e}

Error message

Failed to parse value of `{field}` as integer: {e}

What it means

`parse_int` (entry.rs:253) is used for `worker_threads` (and any integer-valued macro option). It requires a `syn::Lit::Int` and a successful `base10_parse::<usize>()`. A literal that parses syntactically as an integer but overflows `usize` (or any non-integer literal) hits this branch, with the underlying parse error interpolated.

Source

Thrown at tokio-macros/src/entry.rs:260

        Ok(FinalConfig {
            name: self.name.clone(),
            crate_name: self.crate_name.clone(),
            flavor,
            worker_threads,
            start_paused,
            unhandled_panic,
        })
    }
}

fn parse_int(int: syn::Lit, span: Span, field: &str) -> Result<usize, syn::Error> {
    match int {
        syn::Lit::Int(lit) => match lit.base10_parse::<usize>() {
            Ok(value) => Ok(value),
            Err(e) => Err(syn::Error::new(
                span,
                format!("Failed to parse value of `{field}` as integer: {e}"),
            )),
        },
        _ => Err(syn::Error::new(
            span,
            format!("Failed to parse value of `{field}` as integer."),
        )),
    }
}

fn parse_string(int: syn::Lit, span: Span, field: &str) -> Result<String, syn::Error> {
    match int {
        syn::Lit::Str(s) => Ok(s.value()),
        syn::Lit::Verbatim(s) => Ok(s.to_string()),
        _ => Err(syn::Error::new(
            span,
            format!("Failed to parse value of `{field}` as string."),
        )),
    }

View on GitHub (pinned to 625954f365)

Solutions

  1. Provide a plain integer literal within `usize` range, e.g. `worker_threads = 4`.
  2. Do not quote the value — it must be an integer token, not a string.
  3. If the count comes from configuration, read it at runtime via `Builder::new_multi_thread().worker_threads(n)` instead of the macro.

Example fix

// before — string literal or overflow
#[tokio::main(worker_threads = "4")]
async fn main() {}

// after — plain integer literal
#[tokio::main(worker_threads = 4)]
async fn main() {}
Defensive patterns

Strategy: validation

Validate before calling

// Use a plain integer literal in range of usize for the macro:
//   #[tokio::main(worker_threads = 4)]
// For dynamic values, bypass the macro and build manually:
let rt = tokio::runtime::Builder::new_multi_thread()
    .worker_threads(n.clamp(1, 1024))
    .enable_all()
    .build()?;

Prevention

When it happens

Trigger: Writing `#[tokio::main(worker_threads = 99999999999999999999)]` (overflow) or `#[tokio::main(worker_threads = "4")]` (wrong literal kind).

Common situations: Passing a value derived from a string literal; very large counts; negative numbers passed as integer literals.

Understand the failure class

Related errors


AI-assisted analysis of tokio-rs/tokio@625954f365 (2026-08-11). Data as JSON: /api/errors/7ffeed7514f97f9e. Report an issue: GitHub.