beginnerCLI
Command Line Arguments
Parse command line arguments with std::env.
Command Line Arguments
Parse command line arguments with std::env.
Difficulty
Beginner
Code
rust
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
println!("program: {}", args[0]);
println!("args: {:?}", &args[1..]);
if args.len() > 1 {
println!("first arg: {}", args[1]);
}
}Explanation
This example demonstrates how to use command line arguments in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.
Key Concepts
- Rust's strong type system catches errors at compile time
- Ownership and borrowing rules ensure memory safety
- Pattern matching makes code expressive and exhaustive
Related Topics
Browse more examples in the cli category to build a complete understanding of this topic.