1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![allow(unused)]

use std::error::Error;
use std::fmt::{Debug, Display};
use std::io;
use std::str::FromStr;

pub fn input() -> String {
    let mut inner = String::new();
    std::io::stdin()
        .read_line(&mut inner)
        .expect("Failed to read from stdin");
    inner
}

/// Yandros - users.rust-lang.org
/// https://users.rust-lang.org/t/why-is-it-so-difficult-to-get-user-input-in-rust/27444/3
pub fn input_prompt(prompt: &'_ impl Display) -> String {
    print!("{}", prompt);
    let mut ret = String::new();
    std::io::stdin()
        .read_line(&mut ret)
        .expect("Failed to read from stdin");
    ret
}

/// alice - users.rust-lang.org
/// https://users.rust-lang.org/t/why-is-it-so-difficult-to-get-user-input-in-rust/27444/3
pub fn input_ok<T: FromStr>() -> Result<T, Box<dyn Error>>
where
    <T as FromStr>::Err: Error + 'static,
{
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    // Handle the errors outside
    Ok(input.trim().parse()?)
}

pub fn input_from<T: FromStr>() -> T
where
    <T as FromStr>::Err: Debug + 'static,
{
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read from stdin");
    input.trim().parse().unwrap()
}

/// coder3101 - stackoverflow.com
/// https://stackoverflow.com/questions/30355185/how-to-read-an-integer-input-from-the-user-in-rust-1-0
#[macro_export] // export to the root of the crate
macro_rules! read {
    ($out:ident as $type:ty) => {
        let $out = {
            let mut inner = String::new();
            std::io::stdin().read_line(&mut inner).expect("a String");
            inner.trim().parse::<$type>().expect("Parsable")
        };
    };
}

#[macro_export]
macro_rules! read_str {
    ($out:ident) => {
        let $out = {
            let mut inner = String::new();
            std::io::stdin().read_line(&mut inner).expect("a String");
            inner.trim().to_string()
        };
    };
}

#[macro_export]
macro_rules! read_vec {
    ($out:ident as $type:ty) => {
        let $out = {
            let mut inner = String::new();
            std::io::stdin().read_line(&mut inner).unwrap();
            inner
                .trim()
                .split_whitespace()
                .map(|s| s.parse::<$type>().unwrap())
                .collect::<Vec<$type>>()
        };
    };
}