Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active December 29, 2022 12:10
Show Gist options
  • Select an option

  • Save ruxo/4d124d02c28e0cff6560a536441fa841 to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/4d124d02c28e0cff6560a536441fa841 to your computer and use it in GitHub Desktop.
.NET-like Console functions
use std::io::{stdin, Error};
/// Write text output into STDOUT with string format supported. Like .NET `Console.Write()`
#[macro_export]
macro_rules! console_write {
($($args: tt)*) => {{
print!($($args)*);
std::io::Write::flush(&mut std::io::stdout())?;
}};
}
/// Write text output into STDOUT with string format supported. Like .NET `Console.WriteLine()`
#[macro_export]
macro_rules! console_writeline {
($($args: tt)*) => {{
println!($($args)*);
std::io::Write::flush(&mut std::io::stdout())?;
}};
}
/// Read a string from STDIN. Just like .NET `Console.ReadLine()`
pub fn read_line() -> Result<String, Error> {
let mut s = String::new();
let len = stdin().read_line(&mut s)?;
s.truncate(len - 1);
Ok(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment