Skip to main content

Scanner

Struct Scanner 

Source
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>

Source

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());
Source

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");
Source

pub fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>

Reads n tokens and returns them as a vector

§Arguments
  • n - Number of tokens to read
§Examples
use turbo_input::Scanner;
 
let input = "1 2 3 4 5";
let mut scanner = Scanner::new(input.as_bytes());
 
let numbers: Vec<i32> = scanner.vec(5);
assert_eq!(numbers, vec![1, 2, 3, 4, 5]);
Source

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 matrix
  • cols - 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]]);
Source

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']);
Source

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");
Source

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");
Source

pub fn read_lines(&mut self, n: usize) -> Vec<String>

Reads multiple lines as a vector of strings

§Arguments
  • n - Number of lines to read
Source

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 edges
  • directed - 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]);

Auto Trait Implementations§

§

impl<R> Freeze for Scanner<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Scanner<R>
where R: RefUnwindSafe,

§

impl<R> Send for Scanner<R>
where R: Send,

§

impl<R> Sync for Scanner<R>
where R: Sync,

§

impl<R> Unpin for Scanner<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Scanner<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for Scanner<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.