Command Line Arguments are used to control program from outside instead of hard-coding those values inside the code and supplied to program when it is invoked. In this blog, I’ll demystify you how to use Command Line Arguments in Rust Programming Language.
Rust provides us the args() function of the env module defined in the standard library (i.e, std) which returns the iterator of the command line arguments, so here we are going to use args() function to read the arguments.
The first element of the iterator is traditionally path of the executable and the arguments provided by the developer is set from second element onwards, means to the arguments provided by the developer is accessed from the 1st index because indexing of the elements starts from 0.
Passing arguments through Command Line
$ cargo run first_argument second_argument ...
Passing arguments through command line in Rust is just similar to other programming languages, where we pass arguments with the execute the command of the program.
So, here we pass the arguments with cargo run command.
Reading Arguments from Command Line
To read arguments from command line we need to use the args() function of the Rust’s standard library. To use that function first we need to import that function in our program with the help of use keyword.
use std::env
fn main(){
for argument in env::args() {
println!("{}", argument);
}
}
In the above code, we use the args() function to get the arguments and print each of the argument in separate line through a loop, because args() function returns the iterator of the arguments.
After running that program by passing arguments the output of the program looks like:
$ cargo run pawan bisht
Compiling cmd-args v0.1.0 (/home/knoldus/git/knoldus/cmd-args)
Finished dev [unoptimized + debuginfo] target(s) in 3.88s
Running `target/debug/untitled pawan bisht`
target/debug/untitled
pawan
bisht
Access ‘n’th element of the Iterator
We can access the elements of the iterator directly with the nth method of the standard library.
nth() method returns the `n`th element of the iterator.
It will return [`None`] if `n` is greater than or equal to the length of the iterator.
use std::env::args;
fn main() {
let first_argument: String = args().nth(1).unwrap();
let second_argument: String = args().nth(2).unwrap();
println!("{} {}",first_argument, second_argument);
}
Above program get the element of the iterator directly with the nth() method and print the elements after assigning them into variables. Here we start with index 1 because the 0th index of the iterator is preserved for the name of the program.
The output generated by this program is:
$ cargo run pawan bisht
Compiling cmd-args v0.1.0 (/home/knoldus/git/knoldus/cmd-args)
Finished dev [unoptimized + debuginfo] target(s) in 0.28s
Running target/debug/untitled pawan bisht
pawan bisht
Turn Iterator into a Collection
Another way to use the arguments is by turning iterator into a collection by using collect() method of standard library.
collect() method can take anything iterable, and turn it into a relevant collection.
use std::env::args;
fn main() {
let arguments: Vec<String> = args().collect();
let second_argument: String = &arguments[2];
println!("{}", arguments[1]);
println!("{}", secong_argument);
}
Above program uses collect() method to turn the iterator to a vector of string type and then print the values by accessing the values in a vector with their indexes. Here we also store the second argument into a variable and then print the argument.
The output generated by this program is :
cargo run first second
Compiling cmd-args v0.1.0 (/home/knoldus/git/knoldus/cmd-args)
Finished dev [unoptimized + debuginfo] target(s) in 0.37s
Running `target/debug/untitled pawan bisht`
first
second
References
