let mut input = String::new(); io::stdin().read_line(&mut input).expect("Failed"); let input = input.trim(); -> Option B
Quick Check:
read_line needs &mut String and trim() cleans input [OK]
Hint: Use &mut String and call trim() after read_line [OK]
Common Mistakes:
Forgetting &mut before input
Calling trim() on read_line() result
Not creating a mutable String first
3. What will be the output of this Rust code if the user inputs 42?
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed");
let num: i32 = input.trim().parse().expect("Not a number");
println!("Number plus 10 is {}", num + 10);
}
medium
A. Number plus 10 is 52
B. Number plus 10 is 42
C. Not a number
D. Failed
Solution
Step 1: Read and parse input
The input string "42" is trimmed and parsed to integer 42 successfully.
Step 2: Add 10 and print
42 + 10 equals 52, so the output prints "Number plus 10 is 52".
Final Answer:
Number plus 10 is 52 -> Option A
Quick Check:
42 + 10 = 52 [OK]
Hint: Trim and parse input before math operations [OK]
Common Mistakes:
Not trimming input before parse
Parsing input as wrong type
Ignoring parse errors
4. Identify the error in this Rust code snippet that reads an integer from input:
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed");
let num: i32 = input.parse().expect("Not a number");
println!("Number: {}", num);
medium
A. parse() called without trimming input
B. Missing &mut before input in read_line
C. input is not mutable
D. println! macro syntax error
Solution
Step 1: Check read_line usage
read_line correctly uses &mut input and input is mutable.
Step 2: Identify parse error cause
parse() is called on input without trimming, so newline remains causing parse failure.
Final Answer:
parse() called without trimming input -> Option A
Quick Check:
Always trim input before parse() [OK]
Hint: Always trim input string before parsing to number [OK]
Common Mistakes:
Forgetting to trim input before parse
Assuming read_line returns trimmed string
Misusing &mut in read_line
5. You want to read multiple numbers from user input separated by spaces and store them in a vector of integers. Which Rust code correctly does this?
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed");
let numbers: Vec<i32> = input.trim().split_whitespace()
.map(|x| x.parse().expect("Not a number"))
.collect();
println!("Numbers: {:?}", numbers);
}
hard
A. read_line() cannot read multiple numbers at once.
B. Fails because split_whitespace() does not split strings.
C. parse() should be called before split_whitespace().
D. Correctly reads and parses space-separated numbers into a vector.
Solution
Step 1: Read input line and trim
Input is read as a string and trimmed to remove trailing whitespace.
Step 2: Split and parse each number
split_whitespace() splits input by spaces, map parses each piece to i32, collect gathers into vector.
Final Answer:
Correctly reads and parses space-separated numbers into a vector. -> Option D