
Understand it
A shell groups text into arguments before a Rust program sees it. Quotes preserve a space or an empty argument; they are not usually part of the resulting argument. Cargo consumes its options before the double dash and passes the remainder to the executable. The first executable argument is conventionally the program name and is not a user command.
Task Ledger accepts exactly list or add TITLE. Reject an unknown command, a missing title and extra arguments. Trim the title for domain validation, while retaining internal spaces and Unicode. Do not partially add a task before discovering a later invalid option. The independent lab takes only the already-separated user arguments; the companion main passes std::env::args().skip(1).
The route project is Task Ledger: first keep a reproducible source history, then expose its data through a CLI and HTTP boundary, persist it in SQL, diagnose failures and prepare a release. This lesson owns one contract in that project. Run the worked example before changing the starter and explain which input makes the tempting shortcut fail.
Terminal practice: Run cargo run -- add "learn Rust" and cargo run -- add "" against the companion CLI.
The compiler lab is independent of the reading question. Edit solution.rs; the app supplies a separate caller with normal, empty and adversarial inputs. A successful unchanged demonstration does not prove your implementation. After the lab, apply the same contract to the route companion project. Its README separates the on-device adapter from the desktop Cargo/TCP profile.
Read the code
pub fn command(args: &[&str]) -> Result<String, String> {
match args {
["list"] => Ok("list".to_owned()),
["add", title] if !title.trim().is_empty() => {
Ok(format!("add:{}", title.trim()))
}
_ => Err("usage".to_owned()),
}
}
fn main() {
assert_eq!(
command(&["add", " learn Rust 🦀 "]),
Ok("add:learn Rust 🦀".to_owned()),
"case 1"
);
assert_eq!(command(&[]), Err("usage".to_owned()), "case 2");
assert_eq!(
command(&["add", ""]),
Err("usage".to_owned()),
"case 3"
);
assert_eq!(
command(&["list", "extra"]),
Err("usage".to_owned()),
"case 4"
);
assert_eq!(command(&["list"]), Ok("list".to_owned()), "case 5");
println!("worked example passed");
}
The worked implementation exercises preserve arguments and reject ambiguous input. Its tests demonstrate the stated finite contract.
Practice starter
pub fn command(args: &[&str]) -> Result<String,String> {
todo!("implement the contract")
}
Try it
Accept exactly list or add plus one nonblank title. Return list or add:TITLE with the title trimmed; reject every other shape with Err("usage"). Keep the public signature. Open the compiler lab and edit solution.rs; do not edit the private verification caller.
What success looks like: The separate caller accepts the changed solution and rejects unchanged TODO, a wrong implementation and a printed success message.
Check your understanding
Which invocation passes one title containing a space?
Reveal the answer
- cargo run -- add "learn Rust"
- cargo run add learn Rust
- Quotes always become part of the Rust title
The shell supplies argument boundaries; Cargo needs the double dash before application arguments.
Go deeper
Common misconception: cargo run add learn Rust
Parse the entire invocation before applying any side effect.
- Identify the boundary: Preserve arguments and reject ambiguous input. Write down its accepted inputs and observable output.
- Exercise the failure: The shell supplies argument boundaries; Cargo needs the double dash before application arguments.
- Check the independent caller: Accept exactly list or add plus one nonblank title. Return list or add:TITLE with the title trimmed; reject every other shape with Err("usage").
Transfer challenge: Apply this contract to Task Ledger, introduce the failure described in the lesson, and show the failing check before fixing it.
Connections
- Recover One Historical File: Restore narrowly from a known revision
- Resolve Configuration Once: Apply explicit configuration precedence