0
0
Rustprogramming~20 mins

Handling input values in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code reading input?

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?

Rust
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());
}
AEnter your name: Hello, Alex!
BHello, Alex!\nEnter your name:
CEnter your name: Hello, Alex\n!
DHello, Alex!
Attempts:
2 left
๐Ÿ’ก Hint

Remember that print! does not add a newline, so flushing is needed to show the prompt before input.

โ“ Predict Output
intermediate
2:00remaining
What error does this Rust code produce when reading input?

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?

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(input).unwrap();
    println!("You typed: {}", input);
}
Aerror[E0599]: no method named `read_line` found for struct `String`
Berror[E0308]: mismatched types: expected &mut String, found struct String
CNo error, runs correctly
Derror[E0425]: cannot find value `input` in this scope
Attempts:
2 left
๐Ÿ’ก Hint

Check the argument type expected by read_line.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Rust program panic when parsing input?

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?

Rust
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);
}
AProgram panics with a parse error at unwrap()
BProgram prints: Number plus 10 is 10
CProgram prints: Number plus 10 is abc10
DProgram waits for more input indefinitely
Attempts:
2 left
๐Ÿ’ก Hint

What happens if parse() fails and you call unwrap()?

๐Ÿ“ Syntax
advanced
2:00remaining
Which Rust code correctly reads and parses an integer input?

Choose the Rust code snippet that correctly reads a line from input and parses it as an i32 without panicking.

A
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let num: i32 = input.parse().unwrap();
B
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let num: i32 = input.trim().parse().unwrap();
C
let mut input = String::new();
io::stdin().read_line(input).unwrap();
let num: i32 = input.trim().parse().unwrap();
D
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let num: i32 = match input.trim().parse() {
    Ok(n) => n,
    Err(_) => 0,
};
Attempts:
2 left
๐Ÿ’ก Hint

Look for safe parsing that handles errors.

๐Ÿš€ Application
expert
3:00remaining
How many items are in the vector after this input processing?

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?

Rust
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());
}
ACount: 5
BCount: 3
CCount: 4
DCount: 6
Attempts:
2 left
๐Ÿ’ก Hint

Only valid integers are collected. Non-integers are skipped.