0
0
Rustprogramming~10 mins

Handling input values in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling input values
Start Program
Prompt User for Input
Read Input as String
Trim Input
Parse Input to Desired Type
Use Value
End Program
The program asks the user for input, reads it as a string, trims whitespace, tries to convert it to the needed type, and either uses the value or shows an error.
Execution Sample
Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    let num: i32 = input.trim().parse().unwrap();
    println!("You typed: {}", num);
}
This code reads a line from the user, converts it to an integer, and prints it.
Execution Table
StepActionInput/ValueResult/StateOutput
1Initialize empty string 'input'""input = ""
2Read line from user"42\n"input = "42\n"
3Trim input"42\n"trimmed = "42"
4Parse trimmed input to i32"42"num = 42
5Print outputnum = 42You typed: 42
6Program ends
💡 Program ends after printing the parsed integer.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
input"""42\n""42\n""42\n""42\n"
trimmedN/AN/A"42""42""42"
numN/AN/AN/A4242
Key Moments - 2 Insights
Why do we need to trim the input before parsing?
Because the input includes a newline character from pressing Enter, trimming removes it so parsing to a number works correctly (see Step 3 in execution_table).
What happens if the user types something that is not a number?
The parse() call will fail and cause the program to panic because unwrap() expects success. Handling errors would require different code (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'input' after reading the line?
A"42"
B"42\n"
C"\n42"
D""
💡 Hint
Check Step 2 in the execution_table where input is assigned after reading.
At which step does the program convert the string to an integer?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where parse() is called in the execution_table.
If the user input was " 7 \n", what would be the trimmed value?
A"7"
B" 7 "
C"7\n"
D" 7 \n"
💡 Hint
Trimming removes spaces and newline characters as shown in Step 3.
Concept Snapshot
Handling input values in Rust:
- Use String::new() to create input buffer
- Read input with io::stdin().read_line(&mut input)
- Trim input to remove whitespace/newlines
- Parse trimmed string to desired type (e.g., i32)
- Use unwrap() for simple error handling (panics on failure)
- Print or use the parsed value
Full Transcript
This example shows how Rust programs handle user input. First, an empty string variable is created to store input. The program reads a line from the user, which includes the newline character from pressing Enter. To convert this input to a number, the program trims whitespace and newlines. Then it parses the trimmed string into an integer. If parsing succeeds, the program prints the number. If parsing fails, the program panics because unwrap() is used. This step-by-step flow helps beginners see how input is processed safely and converted to usable data.