I am running Rust rustc 1.60.0 (7737e0b5c 2022-04-04) on Windows 10 Pro 64-bit and I am using CLion with the Rust plugin.
I was following the advice of @coder3101 at How to read an integer input from the user in Rust 1.0?
The code coder3101 suggested is:
#[allow(unused_macros)]
macro_rules! read {
($out:ident as $type:ty) => {
let mut inner = String::new();
std::io::stdin().read_line(&mut inner).expect("A String");
let $out = inner.trim().parse::<$type>().expect("Parsable");
};
}
#[allow(unused_macros)]
macro_rules! read_str {
($out:ident) => {
let mut inner = String::new();
std::io::stdin().read_line(&mut inner).expect("A String");
let $out = inner.trim();
};
}
#[allow(unused_macros)]
macro_rules! read_vec {
($out:ident as $type:ty) => {
let mut inner = String::new();
std::io::stdin().read_line(&mut inner).unwrap();
let $out = inner
.trim()
.split_whitespace()
.map(|s| s.parse::<$type>().unwrap())
.collect::<Vec<$type>>();
};
}
And then coder3101 suggests the following code to use the macros: (I numbered the lines for reference in this post)
fn main() {
read!(x as u32); // (1) - this works as expected
read!(y as f64); // (2) - this line causes errors on input
read!(z as char); // (3) - this line causes errors on input
println!("{} {} {}", x, y, z);
read_vec!(v as u32); // (4) -- this works as expected
println!("{:?}", v);
}
I am able to use -- read!(x as u32) to successfully read single u32 integer values.
I am able to use -- read_vec!(v as u32) to successfully read a vector of u32 integer values.
However, when I try to use lines (2) and (3) I get the following error at run-time:
1
1.02
thread 'main' panicked at 'Parsable: ParseFloatError { kind: Empty }', src\main.rs:34:5
I also get similar errors when I try to enter a char at (3) (here, I commented out line (2) before running the code
1
b
thread 'main' panicked at 'Parsable: ParseCharError { kind: EmptyString }', src\main.rs:35:5
Is there a specific way to input/type the f64 and char values on the command line that I am missing? Or, has something changed with the newer version of Rust that is causing issues with these macros?