std_macro_extensions/cin/
macro.rs

1/// Read a line from standard input as a `String`.
2///
3/// # Returns
4/// - A `String` containing the input line (including trailing newline if present).
5#[macro_export]
6macro_rules! cin {
7    () => {{
8        let mut input: String = String::new();
9        let _ = std::io::stdin().read_line(&mut input);
10        input
11    }};
12}
13
14/// Parse input string into a value or a vector of values of a specified type.
15///
16/// # Parameters
17/// - `input`: The input `&str` to be parsed.
18/// - `type`: The target type to parse into.
19///
20/// # Returns
21/// - A single value of the specified type if used in scalar mode.
22/// - A `Vec` of values if used in vector mode.
23#[macro_export]
24macro_rules! cin_parse {
25    ($input: expr, Vec<$type: ty>) => {{
26        let mut res: Vec<$type> = Vec::<$type>::new();
27        for token in $input.split_whitespace() {
28            if let Ok(num) = token.parse::<$type>() {
29                res.push(num);
30            }
31        }
32        res
33    }};
34    ($input: expr, $type: ty) => {{
35        let mut res: $type = Default::default();
36        if let Ok(parse_res) = $input.parse::<$type>() {
37            res = parse_res;
38        }
39        res
40    }};
41}