Recall & Review
beginner
How do you read a line of input from the user in Rust?
You use the
std::io::stdin().read_line(&mut input) method, where input is a mutable String variable to store the input.Click to reveal answer
beginner
What must you do to convert a string input to a number in Rust?
You use the
trim() method to remove whitespace, then parse() to convert the string to a number type, handling possible errors.Click to reveal answer
beginner
Why do you need to declare the input variable as mutable when reading input in Rust?
Because
read_line appends the input to the string, so the variable must be mutable to allow changes.Click to reveal answer
intermediate
What does the
expect method do when handling input in Rust?It stops the program and shows an error message if reading input or parsing fails, helping to catch errors early.
Click to reveal answer
beginner
What is the purpose of using
trim() on input strings?It removes extra spaces and newline characters from the input, which helps avoid errors when converting the input to other types.
Click to reveal answer
Which Rust method reads user input from the keyboard?
✗ Incorrect
The
read_line method reads a line of input from the user and stores it in a mutable string.What does the
trim() method do to a string in Rust?✗ Incorrect
trim() removes spaces and newlines from the start and end of a string.Why do you need to use
mut when declaring the input variable for reading input?✗ Incorrect
The
read_line method modifies the string by adding the input, so it must be mutable.What happens if
parse() fails to convert input to a number?✗ Incorrect
parse() returns a Result type that can be an error if conversion fails.What is the role of
expect() when reading input?✗ Incorrect
expect() helps catch errors by stopping the program with a message if something goes wrong.Explain step-by-step how to read a number input from the user in Rust and convert it to an integer.
Think about reading input as text first, then changing it to a number.
You got /5 concepts.
Why is it important to use trim() before parsing input in Rust?
Imagine the user presses Enter, what extra characters might be included?
You got /3 concepts.