0
0
Rustprogramming~10 mins

Reading input basics in Rust - Interactive Code Practice

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

Complete the code to read a line of input from the user.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().[1](&mut input).expect("Failed to read line");
    println!("You typed: {}", input);
}
Drag options to blanks, or click blank then click option'
Aread_exact
Bread_to_string
Cread_line
Dread
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using read_to_string which reads until EOF, not just one line.
Using read_exact which requires a fixed number of bytes.
2fill in blank
medium

Complete the code to trim whitespace from the input string.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let trimmed = input.[1]();
    println!("Trimmed input: {}", trimmed);
}
Drag options to blanks, or click blank then click option'
Atrim
Btrim_start
Ctrim_end
Dstrip
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using trim_start or trim_end which only remove whitespace from one side.
Using strip which does not exist in Rust String.
3fill in blank
hard

Fix the error in the code to parse the input string into an integer.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let num: i32 = input.[1]().parse().expect("Not a number");
    println!("Number is: {}", num);
}
Drag options to blanks, or click blank then click option'
Aclone
Bto_string
Cparse
Dtrim
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to parse the raw input string with newline, causing parse error.
Using to_string or clone which do not remove whitespace.
4fill in blank
hard

Fill both blanks to create a dictionary (HashMap) from words to their lengths, filtering words longer than 3 characters.

Rust
use std::collections::HashMap;

fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let lengths: HashMap<_, _> = words.iter().map(|word| (word, word.len())).filter(|(_, &len)| len [1] 3).collect();
    println!("{:?}", lengths);
}
Drag options to blanks, or click blank then click option'
A<
B>
C>=
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' or '<=' which filters shorter words.
Using '>=' which includes words of length 3.
5fill in blank
hard

Fill all three blanks to read input, trim it, parse to i32, and print the number.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().[1](&mut input).expect("Failed to read line");
    let num: i32 = input.[2]().parse().expect("Not a number");
    println!("You entered: {}", [3]);
}
Drag options to blanks, or click blank then click option'
Aread_line
Btrim
Cnum
Dparse
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Forgetting to trim before parsing causing errors.
Printing the input string instead of the parsed number.