Skip to content

Instantly share code, notes, and snippets.

@fritschy
Created April 29, 2021 18:15
Show Gist options
  • Save fritschy/cc671eb5d4d4c2fcfbeafebdcebba09e to your computer and use it in GitHub Desktop.
Save fritschy/cc671eb5d4d4c2fcfbeafebdcebba09e to your computer and use it in GitHub Desktop.
#![allow(unused_must_use)]
use std::fmt::Display;
use std::ops::{Add, Sub};
use std::str::FromStr;
#[derive(Copy, Clone)]
struct Stdio {}
impl<T: Display> Add<T> for Stdio {
type Output = Stdio;
fn add(self, d: T) -> <Self as Add<T>>::Output {
print!("{}", d);
self
}
}
impl<T: FromStr> Sub<&mut T> for Stdio {
type Output = Stdio;
fn sub(self, n: &mut T) -> <Self as Sub<&mut T>>::Output {
let mut i = String::new();
std::io::stdin().read_line(&mut i).expect("number");
let i = i.strip_suffix("\n").expect("line");
match T::from_str(&i) {
Ok(x) => *n = x,
_ => panic!("oops"),
}
self
}
}
// What follows is from: https://twitter.com/the6p4c/status/1387440324281200640
fn main() {
let mut number = 0;
let stdio = Stdio {};
stdio + "hello " + "world\n";
stdio + "enter your nuber:\n" - &mut number;
stdio + number + " + 1 = " + (number + 1) + "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment