Accessing Command Line Arguments in Rust

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you can access command line arguments in Rust using the std::env::args function, which returns an iterator that yields a String for each argument. The first argument in the returned vector is the path used to call the program, while the rest of the arguments are the command line parameters. Alternatively, you can use crates like clap for more advanced command line argument handling.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/BasicConceptsGroup(["`Basic Concepts`"]) rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/DataStructuresandEnumsGroup(["`Data Structures and Enums`"]) rust(("`Rust`")) -.-> rust/AdvancedTopicsGroup(["`Advanced Topics`"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("`Variable Declarations`") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("`Method Syntax`") rust/AdvancedTopicsGroup -.-> rust/operator_overloading("`Traits for Operator Overloading`") subgraph Lab Skills rust/variable_declarations -.-> lab-99278{{"`Accessing Command Line Arguments in Rust`"}} rust/function_syntax -.-> lab-99278{{"`Accessing Command Line Arguments in Rust`"}} rust/expressions_statements -.-> lab-99278{{"`Accessing Command Line Arguments in Rust`"}} rust/method_syntax -.-> lab-99278{{"`Accessing Command Line Arguments in Rust`"}} rust/operator_overloading -.-> lab-99278{{"`Accessing Command Line Arguments in Rust`"}} end

Program arguments

Standard Library

The command line arguments can be accessed using std::env::args, which returns an iterator that yields a String for each argument:

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();

    // The first argument is the path that was used to call the program.
    println!("My path is {}.", args[0]);

    // The rest of the arguments are the passed command line parameters.
    // Call the program like this:
    //   $ ./args arg1 arg2
    println!("I got {:?} arguments: {:?}.", args.len() - 1, &args[1..]);
}
$ ./args 1 2 3
My path is ./args.
I got 3 arguments: ["1", "2", "3"].

Crates

Alternatively, there are numerous crates that can provide extra functionality when creating command-line applications. The [Rust Cookbook] exhibits best practices on how to use one of the more popular command line argument crates, clap.

Summary

Congratulations! You have completed the Program Arguments lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like