0
0
Rustprogramming~10 mins

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

Choose your learning style9 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.