EasyIO.rs
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 from stdin 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
makes that trivial while also being fast.
Regular output via println!()
can also be problematic since it is line buffered. This can make output can be surprisingly slow. The OutputWriter
buffers all output which results in a much better performance. See documentation below.
To use these in competitive programming simply download the files from here. Then simply 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;
// ...
:warning: Limitations
This struct sacrifices some functionality for performance:
- This does not support UTF8 strings. It will treat each byte in the input source as a separate character. This is a significant speed up and in competitive programming almost always only ascii is used anyway.
- It will not do any validation on the size of numbers before trying to fit them in a
u32
for example. This is also fine for competitive programming since number bounds are usually given.
Public methods
Constructors
// Constructs an InputReader which reads from stdin.
new
// Constructs an InputReader which reads from the file at the given path.
// Note that this returns a result since it will try to open the file.
from_file
// Constructs an InputReader that reads from the given reader.
from_reader
Reader methods
The following methods are pretty self-explanatory. They read the next thing from the input source.
next_usize
The two string methods return a &str
instead of a String
for optimization reasons.
next_word
If you need a String
that you own you can convert it by doing input.next_word()?.to_string()
.
Other instance methods
// Returns Ok(true) if there is more data to be read, Ok(false) otherwise.
has_more // Changes the internal buffer size. Default: 2^16 bytes
// Will return error if shrinking the buffer will cause data loss.
set_buf_size
OutputWriter
Usage
// import it
use OutputWriter;
// ...
Public methods
Constructors
// Constructs an OutputWriter which writes to stdout.
new
// Constructs an OutputWriter which writes to the file at the given path.
// Note that this returns a result since it will try to open the file.
from_file
// Constructs an OutputWriter that writes to the given writer.
from_reader
Instance methods
// Writes the string to the output source.
write
// Convenience method for writing the given string with a newline appended.
writeln
// Flushes the internal buffer and writes it to the output source.
// Note that this is called on drop so you do not have to do it manually.
flush