Simple way to read input from the terminal (stdio)

⚓ rust    📅 2025-05-26    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 34 days ago. The information described in this article may have changed.

there is some way to read input from the terminal efficiently? i'm doing a little bit of competitive programming to train my rust skills, generally i always need to read input from the terminal and them trim it them split and them parse into a int32, this result into my code being a little longer than it should be, there is some way to read input very quickly? like the println! macro but for input instead? here it's my code i don't know a lot about rust soo i'm unsure if i'm doing something wrong:

use std::io;

fn main() {
    let mut buffer = String::new();
    
    io::stdin().read_line(&mut buffer).unwrap();
    
    let mut things: Vec<&str> = buffer.split_whitespace().collect();
    let [a, b] = &things[..] else {panic!("bad input")};
    let a: i32 = a.parse().unwrap();
    let b: i32 = b.parse().unwrap();
    println!("{a}, {b}");
}

1 post - 1 participant

Read full topic

🏷️ rust_feed