An inference service accepts an optional output-token limit from configuration. The raw field may be missing, contain 4096, or contain many. Those cases have different meanings: no override, a parsed limit, and a configuration error. The return type should preserve all three.
Parsing only when a value exists naturally produces Option<Result<usize, ParseIntError>>. The outer Option records whether the field was supplied. The inner Result records whether the supplied text parsed. Rust’s Option::transpose exchanges that nesting:
fn optional_token_limit(
raw: Option<&str>,
) -> Result<Option<usize>, std::num::ParseIntError> {
raw.map(str::parse::<usize>).transpose()
}
map leaves None alone. For Some(text), it calls parse, producing Some(Ok(limit)) or Some(Err(error)). transpose then maps those states to Ok(None), Ok(Some(limit)), and Err(error), respectively. The error now sits at the outer boundary of the function, where a caller can use ? or attach a useful message identifying the configuration field.
The shape matters when several settings are loaded together. If many is treated as though the field were absent, the service might start with a default that the operator did not choose. Returning Err keeps invalid input visible. Conversely, Ok(None) says only that this setting was absent; deciding whether to inherit a deployment default or reject omission is a separate policy choice. Transpose rearranges the state and does not validate whether a parsed value such as zero is acceptable for the model or provider.
If the service has several numeric settings, attach the field name when lifting this result into the service’s configuration error. A diagnostic that names the bad key lets an operator correct the source without guessing which optional value was present.
The method is most useful exactly where optional presence meets a fallible conversion: an environment value, request field, or optional model setting. Parse first, transpose once, then apply domain checks and defaults at the appropriate layer. That order keeps absence, malformed input, and valid input distinct without a manual match for each field.

The Campfire
No commentsNobody has pulled up a log by this one yet. Be the first to say what you make of it.
Held for the desk. It appears after a look.