0

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?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • I copy/pasted your code and ran it without making any changes and it worked for me, is there something else that you have which isn't shown? – Jeremy Meadows May 18 '22 at 19:32
  • After your reply, I realized this code works on the command line, as expected, when using cargo run -- but fails when running this code in CLion. I will now look for settings in CLion that might be causing the trouble. – Alex Reynolds May 18 '22 at 19:43
  • 2
    Is there any reason these couldn't just be a function? `macro_rules!` are so seldom worth it and are a lot harder to work with. – BallpointBen May 18 '22 at 20:10
  • Here is a non-macro version, which should be preferred when possible: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d18615fe6d06c0316e35a27d19acc0f2 -- note that `read_str()` doesn't trim anymore since that returns a slice. `read_str_trimmed()` is the replacement. With this you'd say something like `let x: u32 = read_as();` or `let x = read_as::();` – cdhowie May 18 '22 at 21:25
  • Would you please include a main function in the playground code you posted to illustrate how to call your functions? The code compiles using your latter syntax -- let x = read_as::() -- but each different type fails during run-time with the error -- thread 'main' panicked at 'Parsable: ParseIntError { kind: InvalidDigit }', src\main.rs:20:29 -- which is the line read_str().parse::().expect("Parsable") – Alex Reynolds May 19 '22 at 00:00
  • @AlexReynolds It looks like the input wasn't a valid digit, then. The playground doesn't support stdin. – cdhowie May 19 '22 at 04:06

0 Answers0