“macro undefined” when reading u8 using scan!()

2019-07-17 19:19发布

问题:

I read about reading integer input in How to read an integer input from the user in Rust 1.0?, but I noticed that all the solutions first take a string as input and then convert it to integer. I wonder if there's a way to read an integer directly.

This page mentions scan!() macro but for some reason it doesn't seem to run when I compile the following program using rustc main.rc.

extern crate text_io;

fn main() {
    let mut a: u8;
    let mut b: u8;
    scan!("{},{}", a, b);
    print!("{} {}", a, b);
}

This produces the error:

error: macro undefined: 'scan!'
    scan!("{},{}",a,b);

回答1:

You have to explicitly say that you want to import macros from this crate:

#[macro_use] extern crate text_io;

This is written at the very top of the readme, you must have missed it.

To use crates from crates.io, you need to add them to your Cargo.toml, for example by adding the following lines to that file:

[dependencies]
text_io = "0.1"


标签: input rust