Struct FastInput

Source
pub struct FastInput { /* private fields */ }
Expand description

Simplifies reading and parsing of known input in a speedy fashion.

Reads all data on standard in into a byte buffer. Provides methods to simplify reading and parsing of lines, specifically aimed to aid in competetive programming where the input is known and correct. Most functions panic if the input is not correct and on the specified format. Note: FastInput assumes *nix line-endings (\n).

FastInput uses interior mutability to allow for zero-copy reading and referencing of string input.

§Examples

Creating a new FastInput and reading two lines:

// Input:
// Hello!
// 12 2000
use fast_input::{FastInput, FastParse};

let input = FastInput::new();
// Must make into String as next_line returns a slice to the internal buffer
// and the second input line advances the internal buffer.
let first_line = input.next_line().to_owned();
let (a, b): (u32, u32) = input.next();

println!("First line was: {}, a + b = {}", first_line, a + b);

Mixing &str with parseables:

To facilitate simple, zero-copy reading of string slices, FastInput defines the Str type. The following example reads (name, age) (&str, u8) pairs and stores them in a HashMap.

use fast_input::{FastInput, FastParse, Str};
use std::collections::HashMap;

// Input:
// Sven 12
// Lorna 22
let input = FastInput::new();
let mut map = HashMap::new();
let (sven, sven_age): (Str, u16) = input.next();
let (lorna, lorna_age): (Str, u16) = input.next();

// Deref the Str to a &str
map.insert(*sven, sven_age);
map.insert(*lorna, lorna_age);
assert_eq!(map["Sven"], 12);

Implementations§

Source§

impl FastInput

Source

pub fn new() -> Self

Creates a new FastInput.

Upon creation the entire contents of standard input will be read. The function will block until EOF is reached. If you are using a terminal you can send EOF using CTRL + D. The initial buffer size is 8196 bytes.

Examples found in repository?
examples/read_into_map.rs (line 6)
4fn main() {
5    println!("Enter String number tuples (hello 2)");
6    let inp = FastInput::new();
7    let mut people = HashMap::new();
8    while inp.has_next_line() {
9        let (name, age): (Str, u16) = inp.next();
10        *people.entry(*name).or_default() = age;
11    }
12    println!("{:?}", people);
13}
More examples
Hide additional examples
examples/read_some_lines.rs (line 12)
8fn solve<'a>() {
9    print!("Enter some text, end with EOF (Ctrl + D): ");
10    std::io::stdout().flush().expect("Failed to flush stdout..");
11
12    let inp = FastInput::new();
13    let line = inp.next_line();
14    println!("The first line was: {}", line);
15    
16    let rest: Vec<_> = inp.lines().collect();
17    println!("There where {} more lines after the first one", rest.len());
18}
Source

pub fn with_buffer_size(buffer_size: usize) -> Self

Creates a new FastInput with a specified buffer size.

For more information, see [new].

Source

pub fn with_reader<T: Read>(input: T) -> Self

Creates a new FastInput with a given input that implements Read

§Examples

Creating a FastInput over a byte slice:

use fast_input::{FastInput, FastParse};
use std::io::Read;

let data = "1 2\n3 4".as_bytes();

let input = FastInput::with_reader(data);

let (one, two) = input.next();
let (three, four) = input.next();

assert_eq!((1, 2), (one, two));
assert_eq!((3, 4), (three, four));
assert_eq!(false, input.has_next_line());

For more information, see [new].

Source

pub fn next_line(&self) -> &str

Reads the next line and returns it.

§Panics

The function panics if there is no more data in the buffer. If you are unsure if there is a next line, see [has_next_line].

Examples found in repository?
examples/read_some_lines.rs (line 13)
8fn solve<'a>() {
9    print!("Enter some text, end with EOF (Ctrl + D): ");
10    std::io::stdout().flush().expect("Failed to flush stdout..");
11
12    let inp = FastInput::new();
13    let line = inp.next_line();
14    println!("The first line was: {}", line);
15    
16    let rest: Vec<_> = inp.lines().collect();
17    println!("There where {} more lines after the first one", rest.len());
18}
Source

pub fn next_parsed<'a, T: FParse<'a>>(&'a self) -> T

Reads the next line as a single value and parses it.

§Examples

Reading an integer:

//Input:
//123
use fast_input::FastInput;

let input = FastInput::new();
let number: i32 = input.next_parsed();
println!("{}", number);
Source

pub fn next_as_iter<'a, T: FParse<'a>>(&'a self) -> impl Iterator<Item = T> + '_

Reads the next line and returns an iterator over the elements of the line.

§Examples

Collecting a line into a Vec of integers.

use fast_input::FastInput;

let input = FastInput::new();
let numbers: Vec<u32> = input.next_as_iter().collect();
println!("Last line contained {} numbers!", numbers.len());
§Panics

If there is no more data in the buffer. See [has_next_line].

Source

pub fn next_split<'a>(&'a self) -> impl Iterator<Item = &'a str> + '_

Reads the next line and returns an iterator over the elements (no parsing).

§Examples

Reading a sentence and printing the individual words:

use fast_input::FastInput;

let input = FastInput::new();
let words = input.next_split();
for (i, word) in words.enumerate() {
    println!("Word {} was: {}", i, word);
}
§Panics

If there is no more data in the buffer. See [has_next_line].

Source

pub fn has_next_line(&self) -> bool

Checks if there is more data available in the buffer.

§Examples

Reading until EOF:

use fast_input::FastInput;

let input = FastInput::new();
while input.has_next_line() {
    println!("{}", input.next_line());
}
Examples found in repository?
examples/read_into_map.rs (line 8)
4fn main() {
5    println!("Enter String number tuples (hello 2)");
6    let inp = FastInput::new();
7    let mut people = HashMap::new();
8    while inp.has_next_line() {
9        let (name, age): (Str, u16) = inp.next();
10        *people.entry(*name).or_default() = age;
11    }
12    println!("{:?}", people);
13}
Source

pub fn lines<'a>(&'a self) -> impl Iterator<Item = &str> + 'a

Returns a (consuming) iterator over all remaining lines.

§Examples

Printing all lines:

use fast_input::FastInput;

let data = "First\nSecond\nThird".as_bytes();
let input = FastInput::with_reader(data);
let all_lines: Vec<_> = input.lines().collect();

assert_eq!(&all_lines, &["First", "Second", "Third"]);
assert_eq!(input.has_next_line(), false);
Examples found in repository?
examples/read_some_lines.rs (line 16)
8fn solve<'a>() {
9    print!("Enter some text, end with EOF (Ctrl + D): ");
10    std::io::stdout().flush().expect("Failed to flush stdout..");
11
12    let inp = FastInput::new();
13    let line = inp.next_line();
14    println!("The first line was: {}", line);
15    
16    let rest: Vec<_> = inp.lines().collect();
17    println!("There where {} more lines after the first one", rest.len());
18}

Trait Implementations§

Source§

impl Default for FastInput

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T1, T2> FastParse<'a, (T1, T2)> for FastInput
where T1: FParse<'a>, T2: FParse<'a>,

Source§

fn next(&'a self) -> (T1, T2)

Reads two elements separated by a space, and returns them parsed as a tuple.

§Examples

Reading an i32 and a f64:

use fast_input::{FastInput, FastParse};

let input = FastInput::new();
let (age, length): (i32, f64) = input.next();
println!("{} {}", age, length);
§Panics

If there is no more data in the buffer. See [has_next_line].

Source§

impl<'a, T1, T2, T3> FastParse<'a, (T1, T2, T3)> for FastInput
where T1: FParse<'a>, T2: FParse<'a>, T3: FParse<'a>,

Source§

fn next(&'a self) -> (T1, T2, T3)

Reads three elements separated by a space, and returns them as a triple.

§Panics

If there is no more data in the buffer. See [has_next_line].

Source§

impl<'a, T1, T2, T3, T4> FastParse<'a, (T1, T2, T3, T4)> for FastInput
where T1: FParse<'a>, T2: FParse<'a>, T3: FParse<'a>, T4: FParse<'a>,

Source§

fn next(&'a self) -> (T1, T2, T3, T4)

Reads four elements separated by a space, and returns them as a quad-tuple.

§Panics

If there is no more data in the buffer. See [has_next_line].

Source§

impl<'a, T1, T2, T3, T4, T5> FastParse<'a, (T1, T2, T3, T4, T5)> for FastInput
where T1: FParse<'a>, T2: FParse<'a>, T3: FParse<'a>, T4: FParse<'a>, T5: FParse<'a>,

Source§

fn next(&'a self) -> (T1, T2, T3, T4, T5)

Reads five elements separated by a space, and returns them as a quintuple.

§Panics

If there is no more data in the buffer. See [has_next_line].

Auto Trait Implementations§

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.