easy_io
Two structs InputReader
and OutputWriter
for fast and convenient IO in Rust.
The main use of these structs is in competitive programming or Kattis style problems. Reading particularly numbers via io::stdin()
is not very convenient. In competitive programming you want to be able to easily get the next number or word in the stream since you know the exact format of the input before hand. The InputReader
struct makes that trivial while also being fast, ensuring IO is not the bottleneck for your solution.
Regular output in Rust via println!()
can also be problematic since it is line buffered. This can make output can be surprisingly slow. The OutputWriter
struct buffers all output until it is flushed/dropped which results in a significant performance improvement.
To use these in competitive programming I would simply download input_reader.rs
and/or output_reader.rs
. Put them in the same folder as your solution and import it like below.
This was inspired by this amazing Java class but written completely separately.
InputReader
Usage
// import it
use InputReader;
// ... or if via crates.io
use InputReader
:warning: Limitations
This struct sacrifices some functionality/correctness for performance and convenience:
- Results are unwrapped internally so that the API is much simpler. In competitive programming you will not recover from any IO error anyway.
- UTF8 strings are not supported. The
InputReader
will treat each byte in the input source as a separate character. This is a significant speed up and in competitive programming only ascii is almost always used anyway. - It will not do any validation on the size of numbers before trying to fit them in a
u8
for example. This is also fine for competitive programming since number bounds are usually given. - It only parses decimal notation for numbers, not hexadecimal or scientific notation for example.
- It will not parse special float values like
NaN
orInfinity
.
Public methods
Constructors
// Constructs an InputReader which reads from stdin.
new
// Constructs an InputReader which reads from the file at the given path.
from_file
// Constructs an InputReader that reads from the given reader.
from_reader
The next()
method
;
// In many cases, the compiler can figure out the type for you.
// Other times you need to supply the type like so:
let a: u32 = input.next;
let b = input.;
This method is how you read most things from the input source. The following types implement the InputReadable
trait and are thus usable with this function.
u64, u32, u16, u8, usize
i64, u32, i16, i8, isize
f64, f32
char, String
Other instance methods
// Returns the next line from the input source.
next_line // Returns true if there is more data to be read from the input source.
has_more // Changes the internal buffer size. Default: 2^16 bytes
set_buf_size
OutputWriter
This struct will simply buffer all output until the until the writer is dropped (or the flush
is called manually).
Usage
// import it
use OutputWriter;
// ... or if via crates.io
use OutputWriter
Public methods
This class implements the Write
trait. This is so we can utilize the write!
and writeln!
macros for easy formatting. This means several more methods are available on the struct though. See documentation here.
Constructors
// Constructs an OutputWriter which writes to stdout.
new
// Constructs an OutputWriter which writes to the file at the given path.
from_file
// Constructs an OutputWriter that writes to the given writer.
from_writer
Instance methods
// Writes something to the output source.
print
// Convenience method for writing something with a space appended.
prints
// Convenience method for writing something with a newline appended.
println