我在 How to read an integer input from the user in Rust 1.0? 中读到了有关读取整数输入的信息,但我注意到所有解决方案首先将字符串作为输入,然后将其转换为整数。我想知道是否有办法直接读取整数。
This page提到 scan!()
宏,但由于某种原因,当我使用 rustc main.rc
编译以下程序时,它似乎没有运行。
extern crate text_io;
fn main() {
let mut a: u8;
let mut b: u8;
scan!("{},{}", a, b);
print!("{} {}", a, b);
}
这会产生错误:
error: macro undefined: 'scan!'
scan!("{},{}",a,b);
最佳答案
您必须明确表示您想要从此 crate 导入宏:
#[macro_use] extern crate text_io;
这是写在自述文件最顶部的,你一定错过了。
要使用 crates.io 中的 crate ,您需要将它们添加到您的 Cargo.toml
中,例如通过将以下几行添加到该文件中:
[dependencies]
text_io = "0.1"
关于input - 使用 scan!() 读取 u8 时为 "macro undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396104/