Last active
September 22, 2023 18:01
-
-
Save Samet195/0ddcfbb439505db87e940c4e0078680a to your computer and use it in GitHub Desktop.
Python's `input()` fn Implementation in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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