expression

Positional arguments: validate the number of inputs

A positional argument is interpreted according to its place. Check arity before using values; missing and surplus arguments should be handled deliberately. This small parser receives arguments after the program name has been removed. Uses a fixed synthetic argument list or local data. Expected stdout (not executed): search rust in notes.txt Common mistake: Indexing args[1] without checking length.

Formal Syntax & Structure

rust syntaxrust
match args.as_slice() { [query, path] => ..., _ => ... }

Working Example

Example Snippetrust
fn main() {    fn parse<'a>(args: &[&'a str]) -> Result<(&'a str, &'a str), &'static str> {        match args { [query, path] => Ok((*query, *path)), _ => Err("expected QUERY FILE") }    }    let (query, path) = parse(&["rust", "notes.txt"]).unwrap();    assert!(parse(&["rust"]).is_err());    println!("search {query} in {path}");}
Rust Reference