angrycder's blog

By angrycder, history, 2 years ago, In English

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

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it