Bird
Raised Fist0
Rustprogramming~10 mins

Why input and output are required in Rust - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a number from the user and print it.

Rust
fn main() {
    let mut input = String::new();
    std::io::stdin().read_line(&mut [1]).expect("Failed to read line");
    println!("You entered: {}", input.trim());
}
Drag options to blanks, or click blank then click option'
Ainput
Boutput
Cbuffer
Dline
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing a variable that is not mutable or not declared.
2fill in blank
medium

Complete the code to convert the input string to an integer.

Rust
fn main() {
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let number: i32 = [1].trim().parse().expect("Please type a number!");
    println!("Number is: {}", number);
}
Drag options to blanks, or click blank then click option'
Anumber
Bline
Cinput
Dbuffer
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to parse a variable that is not a string.
3fill in blank
hard

Fix the error in the code to print the sum of two numbers read from input.

Rust
fn main() {
    let mut input1 = String::new();
    let mut input2 = String::new();
    std::io::stdin().read_line(&mut input1).expect("Failed to read line");
    std::io::stdin().read_line(&mut input2).expect("Failed to read line");
    let num1: i32 = input1.trim().parse().expect("Not a number");
    let num2: i32 = input2.trim().parse().expect("Not a number");
    let sum = num1 + [1];
    println!("Sum is: {}", sum);
}
Drag options to blanks, or click blank then click option'
Anum2
Bnum1
Cinput1
Dinput2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Adding the string variables instead of parsed integers.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 letters.

Rust
fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let lengths: std::collections::HashMap<_, _> = words.iter()
        .filter(|&&word| word.len() [1] 3)
        .map(|&word| (word, word.[2]()))
        .collect();
    println!("{:?}", lengths);
}
Drag options to blanks, or click blank then click option'
A>
B<
Clen
Dto_string
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong comparison operator or wrong method to get length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 4 letters.

Rust
fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let result: std::collections::HashMap<_, _> = words.iter()
        .filter(|&&word| word.len() [1] 4)
        .map(|&word| (word.[2](), word.[3]()))
        .collect();
    println!("{:?}", result);
}
Drag options to blanks, or click blank then click option'
A>
Bto_string
Cto_uppercase
Dlen
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong methods or wrong comparison operators.

Practice

(1/5)
1. Why do Rust programs need input and output?
easy
A. To avoid errors in the code
B. To make the program run faster
C. To get information from users and show results
D. To use more memory

Solution

  1. Step 1: Understand the role of input

    Input allows the program to receive data from users or files, making it flexible and interactive.
  2. Step 2: Understand the role of output

    Output lets the program show results or messages back to the user, completing the interaction.
  3. Final Answer:

    To get information from users and show results -> Option C
  4. Quick Check:

    Input + Output = Interaction [OK]
Hint: Input gets data; output shows results [OK]
Common Mistakes:
  • Thinking input/output speeds up the program
  • Confusing input/output with memory use
  • Believing input/output prevents errors
2. Which Rust code correctly reads a line of input from the user?
easy
A. let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap();
B. let input = input();
C. read_line(input);
D. input = std::io.read_line();

Solution

  1. Step 1: Check Rust input syntax

    Rust uses a mutable String and calls read_line with a mutable reference to store input.
  2. Step 2: Identify correct code

    let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); matches the correct syntax: creating a mutable String, then reading input into it.
  3. Final Answer:

    let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); -> Option A
  4. Quick Check:

    Correct Rust input syntax = let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); [OK]
Hint: Use mutable String and read_line(&mut) for input [OK]
Common Mistakes:
  • Forgetting to make the String mutable
  • Not passing a mutable reference
  • Using undefined functions like input()
3. What will this Rust program output if the user enters "42"?
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!("You entered: {}", num + 1);
}
medium
A. Error: invalid input
B. You entered: 42
C. You entered: 41
D. You entered: 43

Solution

  1. Step 1: Read and parse input

    The program reads input as a string, trims whitespace, and parses it as an integer (42).
  2. Step 2: Add 1 and print output

    The program adds 1 to the number (42 + 1 = 43) and prints "You entered: 43".
  3. Final Answer:

    You entered: 43 -> Option D
  4. Quick Check:

    42 + 1 = 43 [OK]
Hint: Parse input, then add 1 before printing [OK]
Common Mistakes:
  • Not trimming input before parsing
  • Printing input without adding 1
  • Expecting error on valid input
4. This Rust program is supposed to read a number and print it doubled, but it fails. What is the error?
use std::io;
fn main() {
    let mut input = String::new();
    io::stdin().read_line(input).unwrap();
    let num: i32 = input.trim().parse().unwrap();
    println!("Double: {}", num * 2);
}
medium
A. Missing &mut before input in read_line
B. parse() should be parse::<String>()
C. println! macro syntax is wrong
D. input should be immutable

Solution

  1. Step 1: Check read_line argument

    read_line requires a mutable reference (&mut) to the String, but input is passed without &mut.
  2. Step 2: Confirm other parts are correct

    parse() and println! syntax are correct; input must be mutable, so Missing &mut before input in read_line is the error.
  3. Final Answer:

    Missing &mut before input in read_line -> Option A
  4. Quick Check:

    read_line(&mut input) needed [OK]
Hint: Always pass &mut String to read_line [OK]
Common Mistakes:
  • Passing input without &mut
  • Changing parse type incorrectly
  • Miswriting println! syntax
5. You want to write a Rust program that asks the user for their name and age, then prints a greeting with their age next year. Which input and output steps are required?
hard
A. Only print greeting; no input needed
B. Read name and age as input, parse age, then print greeting with age + 1
C. Read age only, ignore name, then print age
D. Read name and age, but do not parse age before printing

Solution

  1. Step 1: Collect both name and age inputs

    The program must read name as a string and age as a string, then parse age to a number.
  2. Step 2: Calculate age next year and print greeting

    Add 1 to the parsed age and print a greeting message including the name and new age.
  3. Final Answer:

    Read name and age as input, parse age, then print greeting with age + 1 -> Option B
  4. Quick Check:

    Input name + age, parse age, output greeting [OK]
Hint: Input all data needed, parse numbers, then output [OK]
Common Mistakes:
  • Skipping parsing age before calculation
  • Ignoring name input
  • Not printing updated age
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard