pub struct Scanner<R> { /* private fields */ }Expand description
A fast scanner for competitive programming
This scanner provides efficient methods for reading various types of input commonly used in competitive programming contests. It buffers input internally to minimize the number of system calls.
§Examples
use std::io;
use turbo_input::Scanner;
let input = "42 3.14 hello\n1 2 3\n";
let mut scanner = Scanner::new(input.as_bytes());
let number: i32 = scanner.token();
let float: f64 = scanner.token();
let text: String = scanner.token();
assert_eq!(number, 42);
assert_eq!(float, 3.14);
assert_eq!(text, "hello");
let vec: Vec<i32> = scanner.vec(3);
assert_eq!(vec, vec![1, 2, 3]);Implementations§
Source§impl<R: BufRead> Scanner<R>
impl<R: BufRead> Scanner<R>
Sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Creates a new Scanner from any type that implements BufRead
§Arguments
reader- Any type implementing BufRead (e.g., stdin, file, string)
§Examples
use std::io;
use turbo_input::Scanner;
// From stdin
let stdin = io::stdin();
let mut scanner = Scanner::new(stdin.lock());
// From string
let input = "1 2 3";
let mut scanner = Scanner::new(input.as_bytes());Sourcepub fn token<T: FromStr>(&mut self) -> T
pub fn token<T: FromStr>(&mut self) -> T
Reads the next token and parses it to the specified type
§Type Parameters
T- The type to parse the token into. Must implement FromStr.
§Panics
Panics if reading fails or if parsing fails.
§Examples
use turbo_input::Scanner;
let input = "42 3.14 hello";
let mut scanner = Scanner::new(input.as_bytes());
let number: i32 = scanner.token();
let float: f64 = scanner.token();
let text: String = scanner.token();
assert_eq!(number, 42);
assert_eq!(float, 3.14);
assert_eq!(text, "hello");Sourcepub fn matrix<T: FromStr>(&mut self, rows: usize, cols: usize) -> Vec<Vec<T>>
pub fn matrix<T: FromStr>(&mut self, rows: usize, cols: usize) -> Vec<Vec<T>>
Reads a matrix of tokens with specified dimensions
§Arguments
rows- Number of rows in the matrixcols- Number of columns in the matrix
§Examples
use turbo_input::Scanner;
let input = "1 2 3\n4 5 6";
let mut scanner = Scanner::new(input.as_bytes());
let matrix: Vec<Vec<i32>> = scanner.matrix(2, 3);
assert_eq!(matrix, vec![vec![1, 2, 3], vec![4, 5, 6]]);Sourcepub fn chars(&mut self) -> Vec<char>
pub fn chars(&mut self) -> Vec<char>
Reads the next token as a string and returns it as a vector of characters
§Examples
use turbo_input::Scanner;
let input = "hello";
let mut scanner = Scanner::new(input.as_bytes());
let chars: Vec<char> = scanner.chars();
assert_eq!(chars, vec!['h', 'e', 'l', 'l', 'o']);Sourcepub fn string(&mut self) -> String
pub fn string(&mut self) -> String
Reads the next token as a string
§Examples
use turbo_input::Scanner;
let input = "hello world";
let mut scanner = Scanner::new(input.as_bytes());
let word1: String = scanner.string();
let word2: String = scanner.string();
assert_eq!(word1, "hello");
assert_eq!(word2, "world");Sourcepub fn read_line(&mut self) -> String
pub fn read_line(&mut self) -> String
Reads a full line as a single string, trimming newline
§Examples
use turbo_input::Scanner;
let input = "line one\nline two";
let mut scanner = Scanner::new(input.as_bytes());
assert_eq!(scanner.read_line(), "line one");
assert_eq!(scanner.read_line(), "line two");Sourcepub fn read_lines(&mut self, n: usize) -> Vec<String>
pub fn read_lines(&mut self, n: usize) -> Vec<String>
Sourcepub fn graph(&mut self, n: usize, m: usize, directed: bool) -> Vec<Vec<usize>>
pub fn graph(&mut self, n: usize, m: usize, directed: bool) -> Vec<Vec<usize>>
Reads a graph representation and returns an adjacency list
§Arguments
n- Number of vertices (vertices are numbered from 1 to n)m- Number of edgesdirected- Whether the graph is directed or undirected
§Returns
A vector of size n+1 where index i contains the neighbors of vertex i. Index 0 is unused to allow 1-based vertex numbering.
§Examples
use turbo_input::Scanner;
// Undirected graph: 1-2, 2-3
let input = "1 2\n2 3";
let mut scanner = Scanner::new(input.as_bytes());
let graph = scanner.graph(3, 2, false);
// graph[1] = [2], graph[2] = [1, 3], graph[3] = [2]
assert_eq!(graph[1], vec![2]);
assert_eq!(graph[2], vec![1, 3]);
assert_eq!(graph[3], vec![2]);