0
0
Rustprogramming~10 mins

Expression evaluation 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 evaluate the sum of two numbers and print the result.

Rust
fn main() {
    let a = 5;
    let b = 3;
    let result = a [1] b;
    println!("Result: {}", result);
}
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using subtraction - instead of addition.
Using multiplication * or division / by mistake.
2fill in blank
medium

Complete the code to calculate the remainder when 10 is divided by 3.

Rust
fn main() {
    let x = 10;
    let y = 3;
    let remainder = x [1] y;
    println!("Remainder: {}", remainder);
}
Drag options to blanks, or click blank then click option'
A%
B/
C+
D*
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using division / instead of remainder %.
Using multiplication or addition by mistake.
3fill in blank
hard

Fix the error in the expression to correctly calculate the power of 2 raised to 3.

Rust
fn main() {
    let base = 2;
    let exponent = 3;
    let power = base.[1](exponent);
    println!("Power: {}", power);
}
Drag options to blanks, or click blank then click option'
A^
B**
C.pow()
Dpow
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using ^ or ** which are not power operators in Rust.
Trying to use a function name without method call syntax.
4fill in blank
hard

Fill in the blank to create a HashMap that maps words to their lengths only if length is greater than 3.

Rust
fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let lengths = words.iter().filter(|&&word| word.len() [1] 3).map(|&word| (word, word.len())).collect::<std::collections::HashMap<_, _>>();
    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 filter wrong words.
Using != which does not check length properly.
5fill in blank
hard

Fill in the two blanks to create a HashMap from words to their uppercase forms only if the word length is less than 6.

Rust
fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let map = words.iter()
        .filter(|&&word| word.len() [1] 6)
        .map(|&word| (word, word.[2]()))
        .collect::<std::collections::HashMap<_, _>>();
    println!("{:?}", map);
}
Drag options to blanks, or click blank then click option'
A>
Bto_uppercase
C<
Dto_lowercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using > instead of < in the filter.
Using to_lowercase() instead of to_uppercase().