Skip to content

Instantly share code, notes, and snippets.

@Samet195
Last active September 22, 2023 18:01
Show Gist options
  • Save Samet195/0ddcfbb439505db87e940c4e0078680a to your computer and use it in GitHub Desktop.
Save Samet195/0ddcfbb439505db87e940c4e0078680a to your computer and use it in GitHub Desktop.
Python's `input()` fn Implementation in Rust
use std::fmt::Display;
use std::io::{stdin, stdout, Write};
pub fn input(prompt: impl Display) -> Box<str> {
let mut buffer = String::new();
stdout().write(prompt.to_string().as_bytes()).unwrap();
stdout().flush().unwrap();
stdin().read_line(&mut buffer).unwrap();
buffer.trim().into()
}
fn main() {
let output = input("Input: ");
println!("Output: {}", output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment