16 个版本
0.1.12 | 2022年7月29日 |
---|---|
0.1.10 | 2022年2月17日 |
0.1.9 | 2021年7月29日 |
0.1.8 | 2020年1月14日 |
0.1.0 | 2015年5月12日 |
#40 在 文本处理
32,753 每月下载量
用于 115 个 crates (103 直接)
12KB
174 行
您可以使用 read!
宏读取单个值并返回它,或者使用 scan!
宏将一个或多个值读取到变量中。这两个宏也可以从文件或从内存中读取。 read!
宏可以接受任何实现了 Iterator<Item=u8>
的类型的任意类型作为可选的第三个参数,并且 scan!
宏的参数可以以 iter =>
前缀开头,其中 iter
实现了 Iterator<Item=u8>
。
示例
scan! 宏
use text_io::scan;
// reading from a string source
let i: i32;
scan!("<b>12</b>".bytes() => "<b>{}</b>", i);
assert_eq!(i, 12);
// reading multiple values from stdio
let a: i32;
let b: &mut u8 = &mut 5;
scan!("{}, {}", a, *b);
read! 宏
use text_io::read;
// read until a whitespace and try to convert what was read into an i32
let i: i32 = read!();
// read until a whitespace (but not including it)
let word: String = read!(); // same as read!("{}")
// read until a newline (but not including it)
let line: String = read!("{}\n");
// expect the input "<b><i>" or panic
// read until the next "<" and return that.
// expect the input "/i></b>"
let stuff: String = read!("<b><i>{}</i></b>");
// reading from files
use std::io::Read;
let mut file = std::fs::File::open("tests/answer.txt").unwrap().bytes().map(|ch| ch.unwrap());
let val: i32 = read!("The answer is {}!!!11einself\n", file);
// reading from strings
let val: i32 = read!("Number: {}", "Number: 99".bytes());