turbo_input

Crates.ioturbo_input
lib.rsturbo_input
version0.1.2
created_at2025-08-02 16:48:11.08007+00
updated_at2025-08-23 13:48:42.545481+00
descriptionA fast scanner library for competitive programming in Rust
homepagehttps://github.com/FukurouHaiRiyo/turbo-input
repositoryhttps://github.com/FukurouHaiRiyo/turbo-input
max_upload_size
id1778984
size18,116
Fuku (FukurouHaiRiyo)

documentation

https://docs.rs/turbo-input

README

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:

    [dependencies]
    turbo-input = "0.1.0"

Quick start

    use std::io;
    use turbo_input::Scanner;

    fn main() {
        let stdin = io::stdin();
        let mut scan = Scanner::new(stdin.lock());
        
        // Read different types
        let n: i32 = scan.token();
        let name: String = scan.string();
        let numbers: Vec<i32> = scan.vec(n as usize);
        
        println!("Read {} numbers: {:?}", n, numbers);
}

Usage/Examples

Basic Token Readin

use turbo_input::Scanner;

let input = "42 3.14 hello";
let mut scanner = Scanner::new(input.as_bytes());

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 = Scanner::new(input.as_bytes());

let n: usize = scanner.token();
let numbers: Vec<i32> = scanner.vec(n);  // [1, 2, 3, 4, 5]

Reading matrices

let input = "1 2 3\n4 5 6\n7 8 9";
let mut scanner = Scanner::new(input.as_bytes());

let matrix: Vec<Vec<i32>> = scanner.matrix(3, 3); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Reading character arrays

let input = "hello";
let mut scanner = Scanner::new(input.as_bytes());

let chars: Vec<char> = scanner.chars();  // ['h', 'e', 'l', 'l', 'o']

Reading graphs

let input = "1 2\n2 3\n1 3";  // 3 edges
let mut scanner = Scanner::new(input.as_bytes());

// Read undirected graph with 3 vertices and 3 edges
let graph = scanner.graph(3, 3, false);
// 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 std::io;
use turbo_input::Scanner;

fn main() {
    let stdin = io::stdin();
    let mut scan = Scanner::new(stdin.lock());
    
    let t: usize = scan.token();  // number of test cases
    
    for _ in 0..t {
        let n: usize = scan.token();
        let arr: Vec<i64> = scan.vec(n);
        
        // Solve the problem...
        let result = solve(&arr);
        println!("{}", result);
    }
}

fn solve(arr: &[i64]) -> i64 {
    // Your solution here
    arr.iter().sum()
}

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

Important Note

This project was made so I can learn Rust and to have a better understanding on howuser input works in Rust, any suggestions/advice and forks are more than welcome.

Changelog

0.1.0

  • Initial release
  • Basic scanner functionality
  • Support for tokens, vectors, matrices, chars, strings, and graphs

0.1.1

  • Bug fixes

0.1.2

  • README changes
Commit count: 21

cargo fmt