5 releases (3 breaking)
0.4.0 | Jun 1, 2019 |
---|---|
0.3.0 | Apr 6, 2018 |
0.2.0 | Feb 4, 2017 |
0.1.1 | Dec 25, 2016 |
0.1.0 | Dec 21, 2016 |
#333 in Parser tooling
443 downloads per month
12KB
199 lines
input-stream
A rust library oferring input streams similar to C++'s *fstreams.
Usage
Module documentation with example.
License
input-stream
is distributed under the MIT license.
See LICENSE.txt for details
lib.rs
:
A small utility library for input parsing in a manner akin to C++'s istream.
It exposes a single struct InputStream
which is wrapped around
any object that implements
std::io::BufRead
.
It can parse any type which implements
std::str::FromStr
.
Usage
This crate is on crates.io and can be used
by adding input-stream
to the dependencies in your project's Cargo.toml
:
[dependencies]
input-stream = "0.3.0"
and this in your crate root:
extern crate input_stream;
Examples:
use std::io;
use input_stream::InputStream;
let mut input = InputStream::new(buf_reader);
let value = input.scan::<bool>();
match value {
Ok(value) => println!("Successfully read boolean: {}", value),
Err(err) => println!("Error reading value: {:?}", err)
}
Reading from standard input:
use std::io;
use input_stream::InputStream;
let stdin = io::stdin();
let mut input = InputStream::new(stdin.lock());
let integer: i32 = input.scan().expect("An integer");
let string: String = input.scan().expect("A string");
println!("Read the number: {} and the string {}", integer, string);
or from a file
use std::io::{self, BufReader};
use std::fs::File;
use input_stream::InputStream;
let mut input = InputStream::new(
BufReader::new(File::open("name_of_file.txt").expect("a file named name_of_file.txt")));
let value: f32 = input.scan().expect("A floating point number");
println!("Read a float: {}", value);