When behavior depends on platform features, build features, feature flags, or user-provided configuration, keep it explicitly scoped and validated.
Apply this checklist:
#[cfg(...)] so non-target platforms don’t even compile unix/fs-only imports.local_fs is off, return a safe default (e.g., None/empty map) without referencing unavailable functionality.Example pattern (cfg + validated parsing):
// Build gating: don't compile unix-only code on Windows.
#[cfg(unix)]
mod local_api;
// Validation: enforce invariants at parse time.
fn parse_positive_usize_query_param(url: &Url, name: &str) -> Result<Option<usize>> {
let Some(raw) = url.query_pairs().find(|(k, _)| k == name).map(|(_, v)| v) else {
return Ok(None);
};
let value = raw.parse::<usize>()?;
ensure!(value > 0, "`{name}` must be greater than 0");
Ok(Some(value))
}
These practices prevent cross-platform build breaks, mis-scoped feature behavior, and silent misconfiguration—while keeping configuration-driven behavior predictable and maintainable.
Enter the URL of a public GitHub repository