Reading input lets your program get information from the user while it runs. This makes your program interactive and useful.
When you want to ask the user for their name and greet them.
When you need to get numbers from the user to do calculations.
When you want to let the user choose options in a menu.
When you want to read commands typed by the user in a console app.
Syntax
Rust
use std::io;
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
You need to create a mutable String variable to store the input.
read_line reads the input and adds it to the String variable.
Examples
This reads a line from the user and prints it back.
Rust
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
println!("You typed: {}", input);
}
This example removes extra spaces and newlines from the input before printing.
Rust
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
let trimmed = input.trim();
println!("Trimmed input: {}", trimmed);
}
Sample Program
This program asks the user for their name, reads it, removes extra spaces, and then greets them.
Rust
use std::io;
fn main() {
println!("Please enter your name:");
let mut name = String::new();
io::stdin().read_line(&mut name).expect("Failed to read line");
let name = name.trim();
println!("Hello, {}!", name);
}
OutputSuccess
Important Notes
Always make the String variable mutable with mut to store input.
Use trim() to remove newline characters from the input.
Handle errors with expect or match to avoid crashes.
Summary
Reading input lets your program talk to the user.
Use a mutable String and io::stdin().read_line() to get input.
Trim the input to clean it before using.
Practice
(1/5)
1. What is the main purpose of using io::stdin().read_line(&mut input) in Rust?
easy
A. To read a line of text input from the user
B. To print text to the console
C. To declare a new variable
D. To create a new thread
Solution
Step 1: Understand the function of read_line
This function reads a line of input from the user and stores it in a mutable string.
Step 2: Identify the purpose of io::stdin()
io::stdin() accesses the standard input stream, allowing the program to receive user input.
Final Answer:
To read a line of text input from the user -> Option A
Quick Check:
Reading input = B [OK]
Hint: Remember: read_line reads user input into a string [OK]
Common Mistakes:
Confusing input reading with printing output
Forgetting to make the string mutable
Using read_line without a mutable reference
2. Which of the following is the correct way to declare a mutable string variable to store user input in Rust?
easy
A. let input: &str = String::new();
B. let input = String::new();
C. let mut input = String::new();
D. let mut input: &str = String::new();
Solution
Step 1: Identify the need for mutability
To store user input, the string must be mutable because read_line modifies it.
Step 2: Check the correct syntax for mutable String
let mut input = String::new(); correctly declares a mutable String variable.
Final Answer:
let mut input = String::new(); -> Option C
Quick Check:
Mutable String declaration = A [OK]
Hint: Use 'mut' to allow input string to change [OK]
Common Mistakes:
Omitting 'mut' keyword for the input string
Using &str instead of String for input storage
Declaring input as immutable
3. What will be the output of this Rust code if the user types "hello" and presses Enter?
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
println!("You typed: {}", input.trim());
}
medium
A. Compilation error
B. You typed: hello
C. You typed:
D. You typed: hello\n
Solution
Step 1: Understand input reading and trimming
The program reads the input including the newline, but trim() removes whitespace like newlines.
Step 2: Analyze the output statement
The trimmed input "hello" is printed after "You typed: ".
Final Answer:
You typed: hello -> Option B
Quick Check:
Trim removes newline, output = D [OK]
Hint: Remember to trim input to remove newline before printing [OK]
Common Mistakes:
Not trimming input, so newline appears in output
Expecting raw input without newline removal
Confusing compile errors with runtime behavior
4. Identify the error in this Rust code snippet that tries to read user input:
use std::io;
fn main() {
let input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
println!("Input: {}", input);
}
medium
A. The variable 'input' should be mutable
B. Missing 'use std::io::Write;' import
C. read_line should not use '&mut' reference
D. println! macro syntax is incorrect
Solution
Step 1: Check variable mutability
read_line requires a mutable reference to modify the string, but input is not mutable.
Step 2: Confirm other parts are correct
Imports and println! syntax are correct; only mutability is missing.
Final Answer:
The variable 'input' should be mutable -> Option A
Quick Check:
Mutable variable needed for read_line = C [OK]
Hint: Make input mutable to allow read_line to change it [OK]
Common Mistakes:
Forgetting 'mut' keyword for input variable
Thinking &mut is not needed for read_line
Assuming println! syntax is wrong
5. You want to read a user's age as an integer from input. Which code snippet correctly reads the input, trims it, and converts it to an i32 in Rust?
hard
A. let age = String::new(); io::stdin().read_line(&age).expect("Failed"); let age: i32 = age.trim().parse().unwrap();
B. let age: i32 = io::stdin().read_line().trim().parse().expect("Invalid number");
C. let mut age = String::new(); io::stdin().read_line(age).expect("Failed"); let age: i32 = age.parse().unwrap();
D. let mut age = String::new(); io::stdin().read_line(&mut age).expect("Failed"); let age: i32 = age.trim().parse().expect("Invalid number");
Solution
Step 1: Read input into a mutable String
Input must be stored in a mutable String variable passed as a mutable reference to read_line.
Step 2: Trim and parse input to i32
Use trim() to remove whitespace, then parse() to convert to integer, handling errors with expect().
Final Answer:
let mut age = String::new(); io::stdin().read_line(&mut age).expect("Failed"); let age: i32 = age.trim().parse().expect("Invalid number"); -> Option D
Quick Check:
Mutable input + trim + parse = A [OK]
Hint: Always trim input before parsing to number [OK]
Common Mistakes:
Not making input string mutable
Forgetting to trim before parsing
Passing input without &mut to read_line
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.