
Understand it
Standard input makes a tool composable: a file can feed the same parser as typed input. Rust strings are UTF-8; byte length and character count answer different questions. A memory budget is measured in bytes, so an emoji can consume several bytes of the budget. A record title is a nonblank line after trimming, with at most 80 Unicode scalar values in this lab.
Reject the complete input if its byte size exceeds the supplied budget or any title is too long. Ignore blank lines and keep record order. A production streaming parser additionally stops accumulation as it reads; this exercise receives an already bounded string to isolate validation. The companion reads through a limited reader and detects one byte beyond its limit. Do not advertise a post-read length check as protection against an unbounded stream.
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: Create input.txt, then run cargo run -- import < input.txt; inspect stderr for an oversized record.
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 titles(
input: &str,
max_bytes: usize,
) -> Result<Vec<String>, String> {
if input.len() > max_bytes {
return Err("too large".to_owned());
}
let mut out = Vec::new();
for line in input.lines() {
let value = line.trim();
if value.chars().count() > 80 {
return Err("title too long".to_owned());
}
if !value.is_empty() {
out.push(value.to_owned());
}
}
Ok(out)
}
fn main() {
assert_eq!(
titles(
" one
two
",
100
),
Ok(vec!["one".to_owned(), "two".to_owned()]),
"case 1"
);
assert_eq!(titles("", 0), Ok(vec![]), "case 2");
assert_eq!(titles("🦀", 3), Err("too large".to_owned()), "case 3");
assert_eq!(
titles(&"x".repeat(81), 100),
Err("title too long".to_owned()),
"case 4"
);
assert_eq!(titles("🦀", 4), Ok(vec!["🦀".to_owned()]), "case 5");
println!("worked example passed");
}
The worked implementation exercises treat input as data with resource limits. Its tests demonstrate the stated finite contract.
Practice starter
pub fn titles(input: &str, max_bytes: usize) -> Result<Vec<String>,String> {
todo!("implement the contract")
}
Try it
Reject input exceeding max_bytes; trim lines, omit blanks, reject titles longer than 80 Unicode scalar values, and preserve order. 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 size controls a UTF-8 input memory budget?
Reveal the answer
- The number of newline separators
- Encoded byte length
- Only the number of visible characters
An emoji can fit one scalar position while exceeding a three-byte budget.
Go deeper
Common misconception: Only the number of visible characters
Check byte limits before retaining input and validate complete records.
- Identify the boundary: Treat input as data with resource limits. Write down its accepted inputs and observable output.
- Exercise the failure: An emoji can fit one scalar position while exceeding a three-byte budget.
- Check the independent caller: Reject input exceeding max_bytes; trim lines, omit blanks, reject titles longer than 80 Unicode scalar values, and preserve order.
Transfer challenge: Apply this contract to Task Ledger, introduce the failure described in the lesson, and show the failing check before fixing it.
Connections
- Resolve Configuration Once: Apply explicit configuration precedence
- Separate Output from Failure: Return observable stdout, stderr and exit status