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