Rust program to print the count of command-line argument using library function

Rust Example: Write a program to print the count of command-line argument using library function.
Submitted by Nidhi, on November 26, 2021

Problem Solution:

In this program, we will count the total number of the command-line argument passed using the len() function and print the result.

Note: The program name is the first argument of "Command Line Arguments".

Program/Source Code:

The source code to print the count of command-line arguments using the library function is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to print the count of 
// command line argument 
// using library function

fn main(){
   let args = std::env::args();    
   println!("Total command line arguments are: {}",args.len());
}

Output:

Compile:
$ rustc main.rs

Execute:
$ ./main One Two Three
Total command line arguments are: 4

Explanation:

In the main() function, we printed the count of command-line arguments using the len() function.

Rust Miscellaneous Programs »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.