Crates.io | whiteread |
lib.rs | whiteread |
version | 0.5.0 |
source | src |
created_at | 2015-06-03 13:15:00.424947 |
updated_at | 2019-11-29 00:13:47.142105 |
description | Crate for easily reading whitespace-separated values from strings or input. |
homepage | |
repository | https://github.com/krdln/whiteread |
max_upload_size | |
id | 2300 |
size | 60,276 |
Yet another crate for easily reading values from strings or input.
It was made to mimic cin >>
functionality
and to be usable for parsing text input in format used in algorithmic contests.
cin >>
).
(Line-aware mode is also supported)Result
, not panics) handling of errors.cargo run
to generate
a concatenated single-file template (see below).Reading an integer from stdin:
let x: i32 = parse_line()?;
Tuples and vectors (nest everything as you like)!
let tup: (i32, f64) = parse_string(" 5 3.14 ")?;
let v: Vec<(String, u8)> = parse_string("one 1 two 2 three 3")?;
Wrapping StdinLock
for non-line-based parsing...
let mut i = Reader::from_stdin_naive();
// (almost) equivalent to scanf("%d%d", &a, &b) or cin >> a >> b
let (a, b): (i32, i32) = i.parse()?;
...or just for speed (the line-buffer will be allocated just once):
while let Some((x, y)) = i.line::<Option<(usize, f32)>>()? {
println!("{} {}", y, x);
}
Reading a file (can also use Reader
for more control):
let number: i32 = parse_file("number.txt")?;
On failure, a rendered error will be provided by default, even when unwrapping, eg.:
Error: excessive input provided at
6 | hello world 1 2 3
^
cargo add whiteread
or add this to your Cargo.toml
:
[dependencies]
whiteread = "0.5.0"
Minimal supported Rust version is 1.18.
If you want to use this crate where cargo is unavailable, whiteread can be squished into single file. Here's how to generate a template containing a whiteread module:
$ cargo install whiteread
$ whiteread-template > my_file.rs
Alternatively, you can clone this repository and just
cargo run
.