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

2019-07-17 18:52发布

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);

标签: input rust
1条回答
狗以群分
2楼-- · 2019-07-17 19:32

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"
查看更多
登录 后发表回答