Configuration and precedence
Quick answer
A CLI can obtain settings from defaults, a config file, environment variables and explicit flags. Define an unambiguous precedence rule so users know which value wins.
Resolve before validating
Here explicit flag overrides environment, then file, then default. Absence and an invalid supplied value are different: a malformed high-priority value should fail rather than silently fall back. Parse and validate the chosen value before performing I/O. Use injected inputs in unit tests instead of mutating the process environment; do not log secret configuration values.
fn choose<'a>(flag: Option<&'a str>, env: Option<&'a str>, file: Option<&'a str>) -> &'a str { flag.or(env).or(file).unwrap_or("10")}fn main() { println!("{}", choose(Some("5"), Some("7"), Some("9"))); println!("{}", choose(None, None, None));}Expected stdout (not executed):
510The function is deterministic and does not touch the environment.
fn limit(s: &str) -> Result<u32, &'static str> { let n: u32 = s.parse().map_err(|_| "not a number")?; if !(1..=100).contains(&n) { return Err("out of range"); } Ok(n)}fn main() { println!("{:?}", limit("0")); println!("{:?}", limit("25")); }Expected stdout (not executed):
Err("out of range")Ok(25)Range checking is separate from parsing.
Common mistake
Do not confuse an empty explicit value with an absent value.
Try it yourself
Return the selected value and its source label without revealing secrets.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Configuration and precedence — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
The flag is absent, the environment value valid, and a file value valid. Which wins under the lesson's precedence?
Configuration and precedence — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn choose<'a>(flag: Option<&'a str>, env: Option<&'a str>, file: Option<&'a str>) -> &'a str { flag.or(env).or(file).unwrap_or("10") } fn main() { println!("{}", choose(Some("5"), Some("7"), Some("9"))); println!("{}", choose(None, None, None)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Configuration and precedence — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn limit(s: &str) -> Result<u32, &'static str> { let n: u32 = s.parse().map_err(|_| "not a number")?; if !(1..=100).contains(&n) { return Err("out of range"); } Ok(n) } fn main() { println!("{:?}", limit("0")); println!("{:?}", limit("25")); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.