ADVERTISEMENT
ADVERTISEMENT

Rust program to read an integer number from the user

Here, we are going to learn how to read an integer number from the user in Rust programming language?
Submitted by Nidhi, on September 22, 2021

Problem Solution:

Here, we will read an integer number from the user and print the input value.

Program/Source Code:

The source code to read an integer number from the user is given below. The given program is compiled and executed successfully.

// Rust program to read an integer 
// number from the user

use std::io;

fn main() {
    let mut num:i32=0;
    let mut input = String::new();
    
    println!("Enter number: ");
    io::stdin().read_line(&mut input).expect("Not a valid string");
    num = input.trim().parse().expect("Not a valid number");
    
    println!("Number is: {}",num);
}

Output:

Enter number: 
108
Number is: 108

Explanation:

Here, we created an integer variable num and then read the value for num from the user using the read_line() function and printed the result.

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.