For example, you can pass the directory you wish to list to the ls command. Hence, the path to the directory is known as the argument for the ls command.
This article will go over the fundamentals of accepting and parsing command line arguments in the Rust programming language using the env module from the standard library.
Setup & Imports
The first step is to create a directory to store our source code. For simplicity, let us generate a Rust package using cargo as:
Rename your project to any name you see fit.
Next, navigate into the source directory and open the main.rs file with your favorite text editor.
The second step is to import the required modules. We will use the env module from the standard library to parse command-line arguments for this article.
The env module provides a function for working with environment variables, arguments, etc. To use it, import it using as:
Argument Vector
Inside the main function, we need to create a vector of strings that will hold the arguments we will pass to the program.
We can do this by adding the following line:
The code above uses the collect method to iterate over the argument passed to the arguments and add them to the vector. The vector is of type strings as annotated by the Vec<Strings> in our case. This is because Rust cannot infer the type of vector collection.
Print Arguments
Now that we can accept command arguments and store them into a vector let us try printing them. We can do this with the iter function, as shown below:
fn main() {
let args: Vec<String> = env::args().collect();
for arg in args.iter() {
println!("{}", arg);
}
}
The iter function will iterate over each argument passed to the program and print it. If we run the code above, we should see an output as:
The program returns the name of the executable. Rust will treat the name of the executable as the first argument. Hence, the argument at index 0 in the argument vector is the path to the name of the program.
This is a common feature in other programming languages, including C, Python, Go, etc.
If we provide other arguments after the name of the program, Rust will append them to index 1, 2, 3… to the argument vector.
For example:
Note that the example above passes 5 arguments to the program. The result is as shown:
argument1
argument2
argument3
...
argumentN
As you can see, the program prints back all the arguments passed.
Accessing and Saving Arguments
Until now, we have only printed the arguments from the program. Although it illustrates how argument vectors work, it doesn’t really do much.
We can access and save each argument to a variable to expand our program.
To access an argument, we can use the index as:
fn main() {
let args: Vec<String> = env::args().collect();
println!("{}", args[1]);
}
In the example above, we access the first argument using index 1. The result is as shown:
We can save the argument into a variable as:
fn main() {
let args: Vec<String> = env::args().collect();
let greet = &args[1];
if greet == "Hi" {
println!("Hi Back!");
} else {
println!("we only accept greetings!")
}
In the program above, we accept to save the first argument and save it in a variable. We then use its value in an if..else block to perform an action.
We can run the code above as:
The program should return “Hi Back!” as:
If we pass another argument that does not equal “Hi”, we executed the else block as:
Conclusion
Fantastic! We have a program that can accept, parse, save, and arguments with that. Keep practicing to learn more.