OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Command-line arguments and exit status

Read arguments without confusing them with source code or terminal syntax.

Lesson 25 of 30 · About 20 minutes · Beginner · English

By the end of this lesson

Read arguments without confusing them with source code or terminal syntax.

Before you start

  • Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
  • Complete lesson 24, or be comfortable explaining and running its examples.

Three words to know

Argument
An input passed when starting a process.
Exit status
A numeric success/failure result returned to the shell.
stderr
The output stream normally used for diagnostics.

1. Understand the idea

The executable name is the first process argument; skip(1) moves past it. args_os preserves operating-system strings, useful for file paths that need not be valid Unicode. With Cargo, -- separates Cargo options from arguments passed to the program. Quote paths containing spaces. A returned output string and an exit status are different things.

2. Save, run and observe

Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.

Require one argumentRUST · save as lesson.rs
fn main() {
    let mut args = std::env::args_os().skip(1);
    match (args.next(), args.next()) {
        (Some(_path), None) => println!("one argument"),
        _ => { eprintln!("error: expected one path"); std::process::exit(2); }
    }
}
What you should see · example output
one argument
Compile and run on macOS/LinuxTERMINAL · Bash or Windows PowerShell
rustc --edition=2024 lesson.rs -o lesson
./lesson tasks.tsv

3. Explain the result

This example checks argument count but does not open the path. Run the executable as ./lesson tasks.tsv on Bash or .\lesson.exe tasks.tsv in PowerShell. Without the argument, it writes the generic diagnostic to stderr and exits with status 2; stdout stays empty. In Cargo use cargo run -- tasks.tsv.

YOUR TURN

A small check before you continue

Why should a file argument named my tasks.tsv be quoted?

Show a suggested answer

Without quotes, the shell can pass it as two arguments. Use "my tasks.tsv" as one argument.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.