gpt4 book ai didi

input - 使用 scan!() 读取 u8 时为 "macro undefined"

转载 作者:行者123 更新时间:2023-11-29 08:04:00 25 4
gpt4 key购买 nike

我在 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 的箱子,您需要将它们添加到您的 Cargo.toml 中,例如将以下行添加到该文件中:

[dependencies]
text_io = "0.1"

关于input - 使用 scan!() 读取 u8 时为 "macro undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com