Rust program to read a string from the user

Rust Example: Write a program to read a string from the user.
Submitted by Nidhi, on November 27, 2021

Problem Solution:

In this program, we will read a string from the user using the read_lin() function and print the input string.

Program/Source Code:

The source code to read a string from the user is given below. The given program is compiled and executed successfully.

// Rust program to read a string 
// from the user

fn main(){
   let mut Str = String::new();
   
   println!("Enter String: ");
   std::io::stdin().read_line(&mut Str);
   
   println!("String is: {}", Str);
}

Output:

Enter String: 
Hello World
String is: Hello World

Explanation:

Here, we created a string variable Str. Then we read the string from the user using the read_line() function and printed the input string.

Rust Miscellaneous Programs »




Comments and Discussions!

Load comments ↻





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