1 个不稳定版本
| 0.1.0 | 2021 年 11 月 15 日 |
|---|
#2157 in Rust 模式
9KB
222 行
scanio
RFC: https://github.com/rust-lang/rfcs/pull/3183
这个包是测试/正在进行中的某些简单宏的实现,用于通用文本输入扫描(与 print 宏系列一起)。
此包当前实现了四个宏,scan!,try_scan!,read! 和 try_read!。
这些宏的实现是实验性的。
scan! 使用方法
#[macro_use]
extern crate scanio;
fn main() {
let name: String;
let age: u8;
// reads a String into `name` and a u8 into `age`
// if it fails, it will simply assign Default::default()
scan!("{} {}", name, age);
println!("{} of age {}", name, age);
}
try_scan! 使用方法
#[macro_use]
extern crate scanio;
fn main() {
// returns a Result<(String, u8), ()>, which we unwrap
let person = try_scan!("{} {}", String, u8).expect("Invalid input!");
// ideally you should `match` on the result but this is an example so :shrug:
println!("{} of age {}", person.0, person.1);
}
read! 和 try_read! 宏的工作方式完全相同,但是它们的第一个参数必须是一个实现了 std::io::Read 特性的对象的可变引用,例如 std::io::stdin() 或一个 File。