Input Array of numbers

and a simple addition program for Competitive Programming and more in Rust

Introduction

For many applications, including Competitive Programming, it is frequently required to take an array of space separated numbers from a user as string and covert it to an Array / Vector of numbers or any desired type.

So, it is better to make a function and just call it instead of rewriting it again and again. It should be included in your template, if you are doing Competitive Programming in Rust.

What are we doing

In Rust, and most other programming languages, input is taken as string of characters. So, if it is written as an array of space separated strings, it is still a string in rust.

We want to separate the numbers and store them in an array or a vector.

Addition Program

So, let's start with designing an addition program. In this program, user can input any number of integers in a single line and output will be sum the sum of all number.

Example :
Input :

123 234 345 456 567

Expected Output :

1725

1. Declare the function and string

You can replace usize with any data type you want, provide it can be parsed.

fn take_vector() -> Vec<usize> {
// Take the string input
let mut input = String::new();
}

2. Take Input into string

Now, to obtain user input, we need to bring the io input/output library into scope. This is present in standard library, called std

use std::io;

Now, we read and store input in input string we created earlier.

io::stdin()
.read_line(&mut input)
.unwrap();

unwrap() specifies that program will panic if, somehow, string cannot be read

3. Converting String into a array

Now, we will convert the string into the array of numbers using map method

let arr: Vec<usize> = input
.trim()
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
// Return the array
return arr;

.trim() is used to trim any leading or trailing whitespaces.
.split_whitespace() is used to split the string, on each whitespace.
.map(|x|) is used to perform operation on each part of string, that we made using above function. Each part is represented as x
.parse() is used to convert the string to number. Interestingly, you don't have to tell the data type in this case, because Rust automatically determines it using return data type. Cool, no?
unwrap() specifies that program will panic if, string can not be converted to number. For example if you input 12h, 23 34 it will panic because it is not convertible to number. You must enter 1 number per line.

4. Putting things together

use std::io;
fn take_vector() -> Vec<usize> {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let arr: Vec<usize> = input.trim().split_whitespace()
.map(|x| x.parse().unwrap()).collect();
return arr;
}

5. Program With driver code

use std::io;
fn take_vector() -> Vec<usize> {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let arr: Vec<usize> = input.trim().split_whitespace()
.map(|x| x.parse().unwrap()).collect();
return arr;
}
// Driver Code
fn main(){
let arr = take_vector();
let mut sum = 0;
for i in 0..arr.len(){
sum+=arr[i];
}
println!("{}", sum);
}

Input :

123 234 345 456 567

Expected Output :

1725

Conclusion

In this article, we designed a function that take an array of space separated numbers from a user as string and covert it to an Array / Vector of numbers or any desired type.

Here is the function to summarize and copy and use purposes

use std::io;
fn take_vector() -> Vec<usize> {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let arr: Vec<usize> = input.trim().split_whitespace()
.map(|x| x.parse().unwrap()).collect();
return arr;
}

Note : We can change the data type of number returned by replacing usize in above function with desired data type.

We also designed a simple addition program, in which user can input any number of integers in a single line and output will be sum the sum of all number.

In my humble opinion, if you are doing competitive programming in rust, you should include this function in your template for quick access.

Thank You