ADVERTISEMENT
ADVERTISEMENT

Rust program to find the sum of all command-line arguments

Rust Example: Write a program to find the sum of all command-line arguments.
Submitted by Nidhi, on November 26, 2021

Problem Solution:

In this program, we will find the sum of all command-line arguments and print the result.

Program/Source Code:

The source code to find the sum of all command-line arguments is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to find the sum of 
// all command line arguments

fn main(){
	let args = std::env::args(); 
	let mut sum=0;
	let mut cnt=0;

	for arg in args {
		if cnt>0{
			sum = sum  + arg.parse::<i32>().unwrap();
		}
		cnt=1;
	}

	println!("Sum is: {}",sum);
}

Output:

Compile:
$ rustc main.rs

Execute:
$ ./main 10 15 20
Sum is: 45

Explanation:

In the main() function, we calculated the sum of all command-line arguments and printed the result.

Rust Miscellaneous Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.