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
argsyields the program name first, so skip it.varreads an environment variable, or errors.- This is enough for a one-flag tool.
Keywords and builtins used here
StringVecfnifletmainreturnu32use
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.
Step 1 of 5 in Arguments, step 1 of 12 in CLI tools & HTTP clients.