100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Input and Output in Rust

Learn how to print output with println! and print!, and read user input using std::io in Rust.

BasicsBeginner9 min readJul 8, 2026
Analogies

Introduction

Rust provides the println! and print! macros for writing formatted output to the console, using curly braces {} as placeholders for values. For reading input, Rust uses the standard library's std::io module, most commonly via std::io::stdin().read_line(&mut buffer) to capture a line of text, which is then typically parsed into the desired type using .trim().parse() combined with error handling.

🏏

Cricket analogy: Announcing the score with println! is like a commentator reading out "{} for {}" placeholders filled with runs and wickets, while capturing a player's shouted target via stdin().read_line(&mut buffer) needs trim().parse() to convert the scribbled note into a usable number.

Syntax

rust
use std::io;

// Output
println!("Hello, {}!", "world");
print!("No newline here");

// Input
let mut input = String::new();
io::stdin()
    .read_line(&mut input)
    .expect("Failed to read line");

let number: i32 = input.trim().parse().expect("Not a number");

Explanation

The println! macro appends a newline after printing, while print! does not; both support {} placeholders that are replaced by the arguments in order, and {:?} for debug-formatting values like tuples or vectors. Reading input requires creating a mutable String buffer and passing it by mutable reference to read_line, which appends the user's input including the trailing newline character. Because read_line returns a Result, it's common to call .expect(...) to handle potential errors, or use proper match/if let error handling in production code. Since input is always read as a String, converting it to a number requires .trim() to remove whitespace and the newline, followed by .parse() to convert it into the target numeric type, which itself returns a Result that must be handled.

🏏

Cricket analogy: println! ends each commentary line with a break like an over completing, while print! keeps commentary flowing mid-over; {:?} debug-prints a whole scoreboard tuple at once, and reading a shouted score via read_line grabs a trailing newline that must be trimmed before parse() converts it, itself returning a Result you .expect() or match on.

Example

rust
use std::io;

fn main() {
    println!("Enter your age:");

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line");

    let age: u32 = match input.trim().parse() {
        Ok(num) => num,
        Err(_) => {
            println!("Invalid number, defaulting to 0");
            0
        }
    };

    println!("In 5 years you will be {} years old.", age + 5);
}

Output

text
Enter your age:
25
In 5 years you will be 30 years old.

Key Takeaways

  • println! adds a trailing newline; print! does not.
  • Curly braces {} are placeholders for formatted values in output macros.
  • std::io::stdin().read_line(&mut buffer) reads a line of text into a String.
  • Input includes a trailing newline, so .trim() should be called before parsing.
  • .parse() converts a string into a numeric type and returns a Result that should be handled.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#InputAndOutputInRust#Input#Output#Syntax#Explanation#StudyNotes#SkillVeris