Consider this Rust program that reads a line from standard input 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 flushing is needed to show the prompt before input.
The program prints the prompt without a newline, flushes output to show it immediately, then reads input. After trimming the newline from input, it prints the greeting on the next line.
Look at this Rust code snippet:
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(input).unwrap();
println!("You typed: {}", input);
}What error will this code cause when compiled?
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(input).unwrap();
println!("You typed: {}", input);
}Check the argument type expected by read_line.
The method read_line expects a mutable reference &mut String, but the code passes input by value. This causes a type mismatch error.
Examine 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 plus 10 is {}", num + 10);
}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 plus 10 is {}", num + 10);
}What happens if parse() fails and you call unwrap()?
Parsing "abc" as an integer fails, so parse() returns an error. Calling unwrap() on an error causes the program to panic and crash.
Choose the Rust code snippet that correctly reads a line from input and parses it as an i32 without panicking.
Look for safe parsing that handles errors.
Option D uses match to handle parse errors safely, returning 0 if parsing fails. Others either panic or have syntax errors.
This Rust program reads a line of space-separated numbers and collects them into a vector:
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let numbers: Vec = input
.trim()
.split(' ')
.filter_map(|s| s.parse().ok())
.collect();
println!("Count: {}", numbers.len());
} If the user inputs: 10 20 abc 30 40x 50 and presses Enter, what will be printed?
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let numbers: Vec<i32> = input
.trim()
.split(' ')
.filter_map(|s| s.parse().ok())
.collect();
println!("Count: {}", numbers.len());
}Only valid integers are collected. Non-integers are skipped.
The input splits into: "10", "20", "abc", "30", "40x", "50". Only "10", "20", "30", and "50" parse as integers. So the vector length is 4.