typestar

Arguments the plain way in Rust

Before any crate: std::env gives you the arguments and the environment.

use std::env;

fn main() {
    let args: Vec<String> = env::args().skip(1).collect();
    if args.is_empty() {
        println!("usage: report <lang> [limit]");
        return;
    }

    let lang = &args[0];
    let limit: u32 = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(60);
    let theme = env::var("TYPESTAR_THEME").unwrap_or("default".to_string());

    println!("{lang} for {limit}s using {theme}");
}

How it works

  1. args yields the program name first, so skip it.
  2. var reads an environment variable, or errors.
  3. This is enough for a one-flag tool.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
372
Tokens
123
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 74 seconds.

Type this snippet

Step 1 of 5 in Arguments, step 1 of 12 in CLI tools & HTTP clients.

Next →