Input and output

In most contests, standard streams are used for reading input and writing output. In Rust, the standard streams are std::io::Stdin for input and std::io::Stdout for output. The standard rust macros used for output are println!() and print!() but these two macros don't flush the stream at the end of the write, Rust Competitive Helper introduce two different macros to handle the output:

  • out_line!() to print the ouput with newline \n
  • out!() to print an inline output

The input for the program usually consists of numbers and strings separated by spaces or newlines. They can be read into the solve() function as follows:

fn solve(input: &mut Input, _test_case: usize) {
    let len_a = input.read::<usize>();
    let mut a = input.read_vec::<usize>(len_a);
}

The combination of these 2 functions always works, assuming that there is at least one space or newline between each element in the input. For example, the above code can read the following inputs:

4
1 4 3 2

and this piece of code:

fn solve(input: &mut Input, _test_case: usize) {
    let a = input.read::<usize>();
    let b = input.read::<usize>();
    let c = input.read::<String>();
}

can read both of the following inputs:

123 456 monkey
123 456
monkey

the input argument in solve() function is managed by Rust Competitive Helper, the possible cases are 3:

  • Single test: the input variable represent the only test
  • Number of test given: the input represent each single test, in this case the very first line (the number of tests indeed) is automatically read and used for reading loop but ignored in input variable
  • Read until EOF: the input represents each test but in this case the number of tests is unknown