Consider this Rust program that reads a line from the user and prints it back:
use std::io::{self, Write};
fn main() {
let mut input = String::new();
print!("Enter your name: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).unwrap();
println!("Hello, {}!", input.trim());
}If the user types Alex and presses Enter, what will be printed?
use std::io::{self, Write};
fn main() {
let mut input = String::new();
print!("Enter your name: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).unwrap();
println!("Hello, {}!", input.trim());
}Remember that print! does not add a newline, so the prompt stays on the same line.
The print! macro prints the prompt without a newline, so the cursor stays on the same line. The user types Alex and presses Enter. The program then prints Hello, Alex! with a newline. So the full output is the prompt and the greeting on the same line, followed by a newline.
Look at this Rust code snippet:
use std::io;
fn main() {
let input = String::new();
io::stdin().read_line(&mut input).unwrap();
println!("You typed: {}", input);
}What error will the compiler produce?
use std::io;
fn main() {
let input = String::new();
io::stdin().read_line(&mut input).unwrap();
println!("You typed: {}", input);
}Think about whether the variable can be changed when passed as mutable reference.
The variable input is declared as immutable by default. The read_line function requires a mutable reference to modify the string. So the compiler complains that you cannot borrow an immutable variable as mutable.
Consider this Rust program:
use std::io;
fn main() {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
let numbers: Vec = buffer
.trim()
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
println!("Sum: {}", numbers.iter().sum::());
} If the user inputs 10 20 30 and presses Enter, what will be printed?
use std::io;
fn main() {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
let numbers: Vec<i32> = buffer
.trim()
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
println!("Sum: {}", numbers.iter().sum::<i32>());
}Think about how the input string is split and parsed into numbers before summing.
The input string is split by whitespace into three strings: "10", "20", "30". Each is parsed into an integer and collected into a vector. The sum of these integers is 10 + 20 + 30 = 60.
Look at this Rust code:
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!("Number is {}", num);
}If the user types abc and presses Enter, what happens?
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!("Number is {}", num);
}What happens if you try to convert a non-number string to an integer?
The parse() method tries to convert the trimmed input to an integer. Since "abc" is not a valid number, unwrap() causes the program to panic with a parse error.
You want to read multiple lines from standard input until there is no more input (EOF). Which Rust code snippet correctly does this?
Think about how to handle input lines safely and efficiently until EOF.
Option C uses io::stdin().lock().lines() which returns an iterator over lines until EOF. It unwraps each line safely and prints it. This is the idiomatic and correct way to read multiple lines until EOF in Rust.
Option C is close but reuses the same string without clearing it properly before reading, which can cause concatenation issues.
Option C checks if the input string is empty, but read_line returns 0 on EOF, not an empty string, so it may loop infinitely.
Option C does not clear the input string, so it will print accumulated input repeatedly.