turbo-input
A fast and efficient scanner library for competitive programming in Rust. This library provides convenient methods for reading various types of input commonly encountered in programming contests.
Features
- Fast input reading with internal buffering
- Type-safe parsing with automatic type inference
- Common data structures like vectors, matrices, and graphs
- Zero dependencies - uses only the Rust standard library
- Comprehensive documentation with examples
- Thoroughly tested with unit tests
Installation
Add this to your Cargo.toml:
Quick start
use io;
use Scanner;
Usage/Examples
Basic Token Readin
use Scanner;
let input = "42 3.14 hello";
let mut scanner = new;
let number: i32 = scanner.token; // 42
let float: f64 = scanner.token; // 3.14
let text: String = scanner.string; // "hello"
Reading vectors
let input = "5\n1 2 3 4 5";
let mut scanner = new;
let n: usize = scanner.token;
let numbers: = scanner.vec; // [1, 2, 3, 4, 5]
Reading matrices
let input = "1 2 3\n4 5 6\n7 8 9";
let mut scanner = new;
let matrix: = scanner.matrix; // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Reading character arrays
let input = "hello";
let mut scanner = new;
let chars: = scanner.chars; // ['h', 'e', 'l', 'l', 'o']
Reading graphs
let input = "1 2\n2 3\n1 3"; // 3 edges
let mut scanner = new;
// Read undirected graph with 3 vertices and 3 edges
let graph = scanner.graph;
// graph[1] = [2, 3], graph[2] = [1, 3], graph[3] = [2, 1]
API Reference
Scanner
The main scanner struct that wraps any type implementing BufRead. Methods:
- new(reader: R) -> Self - Creates a new scanner
- token() -> T - Reads and parses the next token
- vec(n: usize) -> Vec - Reads n tokens into a vector
- matrix(rows: usize, cols: usize) -> Vec<Vec> - Reads a matrix
- chars() -> Vec - Reads next token as character vector
- string() -> String - Reads next token as string
- graph(n: usize, m: usize, directed: bool) -> Vec<Vec> - Reads a graph
All parsing methods support any type that implements FromStr, including:
- Integers: i32, i64, u32, u64, usize, etc.
- Floating point: f32, f64
- Strings: String And more!
Performance
This scanner is designed for competitive programming where fast I/O is crucial. It:
- Buffers input internally to minimize system calls
- Uses efficient string parsing
- Minimizes memory allocations where possible
Typical Competitive Programming Usage
use io;
use Scanner;
Error handling
Currently, the scanner will panic on:
- I/O errors (failed reads)
- Parse errors (invalid format for requested type)
This is intentional for competitive programming where you want fast failure on invalid input rather than error handling overhead.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under
- MIT license
Changelog
0.1.0
- Initial release
- Basic scanner functionality
- Support for tokens, vectors, matrices, chars, strings, and graphs