Basic Template for Competitive Programming in Rust

includes Input functions for various data types necessary for Competitive Programming in Rust

Introduction

This article is specifically for people that are starting Competitive Programming in Rust

In this article, we will put together the programs written in previous articles to make a nicely designed and simple template for Competitive Programming in Rust for speedy submissions.

Feel free to use it. If you like it, you can follow me on GitHub . If you find any issue, Report the issue here.

I hope you like it!

Basic I/O Template

/*
This template is made by Naman Garg <[email protected]>
GitHub : https://github.com/namanlp
GitLab : https://gitlab.com/namanlp
Website : https://rustp.org
You can visit https://rustp.org/basic-programs/basic-template/
for understanding the template
Feel free to copy the template, but not the solutions :D
Thank You
*/
#![allow(unused)]
use std::io::stdin;
fn take_int() -> usize {
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
return input.trim().parse().unwrap()
}
fn take_vector() -> Vec<usize> {
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
let arr: Vec<usize> = input.trim().split_whitespace().map(|x| x.parse().unwrap()).collect();
return arr;
}
fn take_string() -> Vec<char> {
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
let vec:Vec<char> = input.trim().chars().collect();
return vec;
}
fn to_string(vec:Vec<char>) -> String{return vec.iter().collect::<String>();}
fn solve() {
// ======================= Code Here =========================
}
pub fn main() {
let t = take_int();
for _ in 0..t { solve(); }
}

You can take the input of space separated numbers as vector of numbers, and then access the individual numbers using indexing, that is, square brackets [].

I personally prefer this instead of making scanner functions, simply because they are more handy.

Conclusion

In this article, I shared a basic template for input and output for simple competitive programming questions in Rust. This template might be updated in the future.

Next, we should practice some competitive programming questions using Rust for better understanding.

Thank You