Skip to content

Instantly share code, notes, and snippets.

@chandan-g-bhagat
Created Oct 6, 2021
Embed
What would you like to do?
Function to get the input from stream
fn main() {
print!("Please enter some text: ");
let input=get_input();
println!("Input = {0}", input);
}
fn get_input() -> String {
use std::io::{stdin,stdout,Write};
let mut s=String::new();
let _=stdout().flush();
stdin().read_line(&mut s).expect("Did not enter a correct string");
if let Some('\n')=s.chars().next_back() {
s.pop();
}
if let Some('\r')=s.chars().next_back() {
s.pop();
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment