Function to Input a single number

For Competitive Programming in Rust

Introduction

In Competitive Programming, it is generally the very first step to take a number as an input and store it as a number for multiple operations, like addition, subtraction, multiplication, division etc.

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

What are we doing

In Rust, and most other programming languages, input is taken as string of characters. So, it is necessary to convert the string into desired numerical data type like i8, u8 , i16, u16, isize, usize, f32, f64 etc.

Note : In this program, we will assume that integers are in different lines and only 1 integer per line is allowed. For multiple integers in a single line, see Function to Input array of numbers

Simple Multiplication Program

So, let's start with designing a multiplication program. In this program, we will take two integers and output will be the product of two numbers.

Example :
Input :

23
34

Expected Output :

782

1. Declaring function

As you might already know, functions are declared in rust using fn keyword. Also, our function should return the input number. So, our function should look like

fn take_int() -> usize {
// Function body here
}

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

2. Declaring a string

Now, we need a string that is used to store the input. Let us name it input itself

let mut input = String::new();

3. Reading into the 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

4. Converting String into a number

Now, we finally convert the string input to number and return the value.

return input
.trim()
.parse()
.unwrap();

.trim() is used to trim any leading or trailing whitespaces.
.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.

5. Putting things together

use std::io;
fn take_int() -> usize {
// Declare string
let mut input = String::new();
// Input string
io::stdin().read_line(&mut input).unwrap();
// Return number
return input.trim().parse().unwrap();
}

6. Program With driver code

use std::io;
fn take_int() -> usize {
// Declare string
let mut input = String::new();
// Input string
io::stdin().read_line(&mut input).unwrap();
// Return number
return input.trim().parse().unwrap();
}
// Driver code
pub fn main() {
let number1 = take_int();
let number2 = take_int();
println!("{}", number1 * number2);
}

Input :

23
34

Output :

782

Conclusion

In this article, we designed a function that takes input from user and returns a number in any desired numerical data type. Here is the function

use std::io;
fn take_int() -> usize {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
return input.trim().parse().unwrap();
}

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

We also designed a basic multiplication program, which takes input from user and output the product of these 2 numbers.

In next article, we will discuss how to Input an array of numbers in Rust, which is more tricky, but is often required in competitive programming.

Thank You