0
0
Rustprogramming~10 mins

Reading input basics in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading input basics
Start Program
Prompt User
Read Input Line
Store Input in Variable
Use Input
End Program
The program starts, asks the user for input, reads a line from the keyboard, stores it, then uses it before ending.
Execution Sample
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.trim());
}
This Rust program reads a line of text from the user and prints it back.
Execution Table
StepActionInput/ValueVariable StateOutput
1Initialize mutable String variable 'input'N/Ainput = ""
2Prompt user to type inputUser sees prompt (implicit)input = ""
3Read line from stdin into 'input'User types: Hello Rust!input = "Hello Rust!\n"
4Trim newline from 'input'N/Ainput = "Hello Rust!\n"
5Print trimmed inputN/Ainput = "Hello Rust!\n"You typed: Hello Rust!
6Program endsN/Ainput = "Hello Rust!\n"
💡 Program ends after printing the user's input.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
input"""Hello Rust!\n""Hello Rust!\n""Hello Rust!\n"
Key Moments - 3 Insights
Why do we need to make 'input' mutable with 'mut'?
Because read_line changes the content of 'input', it must be mutable. See step 1 and 3 in execution_table where 'input' starts empty and then stores user input.
Why does the printed output not show a new line after the input?
The input includes a newline character '\n' from pressing Enter. We use 'trim()' in step 5 to remove it before printing.
What happens if reading input fails?
The program calls 'expect' which will stop the program and show the error message 'Failed to read line'. This is not shown in the table because input succeeded.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'input' after reading?
A""
B"Hello Rust!"
C"Hello Rust!\n"
DAn error message
💡 Hint
Check the 'Variable State' column at step 3 in execution_table.
At which step does the program print the user's input?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Output' column in execution_table to find when output occurs.
If we remove 'mut' from 'input', what will happen when running the program?
ACompilation error because 'input' is not mutable
BProgram will run normally
CRuntime error when reading input
DInput will be empty
💡 Hint
Refer to key_moments about why 'input' must be mutable.
Concept Snapshot
Reading input in Rust:
- Create a mutable String: let mut input = String::new();
- Read line: io::stdin().read_line(&mut input).expect("error");
- Input includes newline, use input.trim() to clean
- Use input as needed
- Program ends after processing input
Full Transcript
This Rust program starts by creating a mutable empty string variable called 'input'. It then waits for the user to type something and press Enter. The typed line, including the newline character, is stored in 'input'. The program trims the newline before printing the message 'You typed: ' followed by the user's input. Finally, the program ends. If reading input fails, the program stops with an error message. The key points are that the input variable must be mutable because it changes, and the newline character from pressing Enter is included in the input and usually trimmed before use.