Rust Command line arguments in main()

Rust-Programs

In this article, Today we will learn, Rust Command line arguments in main() and how we can pass the command line arguments and also how we can iterate or print them when they get executed in the main() function with examples. So let us begin with what are command-line arguments in Rust.

We are going to learn about Command line arguments in the Rust language. Like in C and C++ when we invoke the main() function. we can pass the command line arguments, similarly in Rust also we can pass the command line arguments.

Command-line argument Example


To understand different command-line arguments related to programs, we will take the below example. Let us say, we have a program that searches a text in a file. So we will have two command-line arguments one is, the text we want to find, and the second is the file name in which file we want to find this text. So when we will call this program from the command line then our call will be:

> cargo run findme Testfile.txt
or
> rustc -o main main.rs
> ./main findme testfile.txt

Here we are passing 2 command-line arguments as “findme” and “Testfile.txt”.

1. How to access Command-line arguments


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

Rust Program to access command-line arguments

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    println!("{:?}", args);
}

Output

> rustc -o main main.rs
> ./main findme testfile.txt
["./main", "findme", "testfile.txt"]

2. How to Iterate over command-line arguments


A simple program, which demonstrates how to iterate over the arguments, which are passed during the program invocation.

Rust Program to Pass Command line arguments

use std::env;

fn main() {
    for (i, argument) in env::args().enumerate() {
        println!("{}: {}", i, argument);
    }
}

Output

> ./main findme testfile.txt
0: ./main
1: findme
2: testfile.txt

If you see the output we got three values. These are explained as:

  • 0: ./main : The first argument is the path that was used to call the program.
  • 1: findme : The second argument is the first argument passed to the main function.
  • 2: testfile.txt : The Third argument is the second argument to the main function.

3. Simple program to demonstrate arguments passed


In these examples, we will see two cases, where we will see how we invoke a program with no command-line arguments and when we invoke with some command-line arguments.

3.1 When called with no arguments:

If you see the output, we are just calling the main() function with 0 arguments.

Rust Language Program to called with no arguments

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..]);
}

Output

> rustc -o main main.rs
> ./main
My path is ./main.
I got 0 arguments: [].


3.2 Rust Program When called with argument


If you see the output, we are calling the main() function with 2 arguments.

Rust Language Program

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..]);
}

Output

> ./main findme testfile.txt
My path is ./main.
I got 2 arguments: ["findme", "testfile.txt"].

Conclusion

In this program, we learned how to pass command-line arguments to a Rust program, how to access the command line arguments in the main() function. Also we saw how to iterate on command line arguments in the Rust program.