Recall & Review
beginner
How do you read a line of input from the user in Rust?
Use
std::io::stdin().read_line(&mut input) where input is a mutable String variable.Click to reveal answer
beginner
What type must the variable be when reading input with
read_line?It must be a mutable
String because read_line appends the input to the string.Click to reveal answer
beginner
Why do you need to use
trim() after reading input?trim() removes whitespace and newline characters from the input string, making it clean for parsing or display.Click to reveal answer
intermediate
How do you convert a string input to a number in Rust?
Use
input.trim().parse::<type>() where type is the number type like i32. Handle the Result to catch errors.Click to reveal answer
intermediate
What does
expect do when reading input or parsing?expect stops the program with a message if reading or parsing fails, helping to catch errors early.Click to reveal answer
Which Rust function reads a line from standard input?
✗ Incorrect
The correct way to read a line is using
std::io::stdin().read_line(&mut input).What type should the variable be when using
read_line?✗ Incorrect
read_line requires a mutable String to store the input.Why do we use
trim() on input strings?✗ Incorrect
trim() removes whitespace and newline characters from the input.How do you convert a trimmed string to an integer in Rust?
✗ Incorrect
Use
parse::() on the trimmed string to convert it to an integer.What happens if
expect fails during input reading?✗ Incorrect
expect stops the program and shows the error message if reading or parsing fails.Explain step-by-step how to read a line of input from the user and convert it to an integer in Rust.
Think about reading, cleaning, converting, and error handling.
You got /5 concepts.
Why is it important to use a mutable String when reading input in Rust?
Consider how Rust treats variables and memory safety.
You got /4 concepts.