16 releases

0.1.12 Jul 29, 2022
0.1.10 Feb 17, 2022
0.1.9 Jul 29, 2021
0.1.8 Jan 14, 2020
0.1.0 May 12, 2015

#62 in Text processing

Download history 4470/week @ 2024-01-10 5576/week @ 2024-01-17 4776/week @ 2024-01-24 5164/week @ 2024-01-31 6941/week @ 2024-02-07 8318/week @ 2024-02-14 7685/week @ 2024-02-21 7795/week @ 2024-02-28 8174/week @ 2024-03-06 6513/week @ 2024-03-13 9266/week @ 2024-03-20 7522/week @ 2024-03-27 8872/week @ 2024-04-03 8070/week @ 2024-04-10 9969/week @ 2024-04-17 7423/week @ 2024-04-24

35,840 downloads per month
Used in 113 crates (101 directly)

MIT/Apache

12KB
174 lines

[Build Status Latest Version

You can use either the read! macro to read a single value and return it, or the scan! macro to read one or more values into variables. Both macros can also read from a file or from memory. The read! macro can take any type that implements Iterator<Item=u8> as an optional third argument, and the scan! macro's arguments can be prefixed with iter => where iter implements Iterator<Item=u8>.

Examples

scan! macro

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! macro

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

No runtime deps