line input functions for rust

Revision en1, by angrycder, 2021-10-17 13:26:51

Rust is a viable programming language to compete in competitive programming but one of the difficulty that beginners (in rust) face is taking line input . I have added line input functions with templates. Hope it helps you.

use std::io;
use std::str::FromStr;

#[allow(dead_code)]
fn read_line() -> String {
    let mut buffer = String::new();
    io::stdin()
        .read_line(&mut buffer)
        .expect("failed to read line");
 
    buffer
}

#[allow(dead_code)]
fn read<T : FromStr>() -> Result<T, T::Err>{
    read_line().trim().parse::<T>()
}

#[allow(dead_code)]
fn read_vec<T : FromStr>() -> Result< Vec<T>, T::Err>{
    read_line().split_whitespace().map(|x| x.parse::<T>()).collect()
}

fn solve(){

}

fn main() {
    let t = read::<i32>().unwrap();
    for _i in 0..t {
        solve();
    }
}
Tags rust

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English angrycder 2021-10-17 13:26:51 893 Initial revision (published)