0
0
Rustprogramming~10 mins

If expression 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 print "Positive" if the number is greater than zero.

Rust
let number = 5;
if number [1] 0 {
    println!("Positive");
}
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
Using '==' checks for equality, not greater than.
2fill in blank
medium

Complete the code to assign the string "even" or "odd" based on the number.

Rust
let number = 4;
let result = if number % 2 [1] 0 { "even" } else { "odd" };
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '!=' will assign 'even' to odd numbers.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix the error in the if expression to correctly assign the maximum of two numbers.

Rust
let a = 10;
let b = 20;
let max = if a [1] b { b } else { a };
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '>' will assign a as max incorrectly.
Using '==' or '!=' does not find the maximum.
4fill in blank
hard

Fill in the blank to create a dictionary with word lengths for words longer than 3 letters.

Rust
let words = ["cat", "house", "dog", "elephant"];
let lengths = words.iter()
    .filter(|&word| word.len() [1] 3)
    .map(|word| (word, word.len()))
    .collect::<std::collections::HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
A!=
B>
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' will select shorter words.
Using '==' will select only words of length 3.
5fill in blank
hard

Fill the blanks to create a HashMap of uppercase words and their lengths for words longer than 4 letters.

Rust
let words = ["apple", "bat", "carrot", "dog"];
let result = words.iter()
    .filter(|&word| word.len() [1] 4)
    .map(|word| (word.[2](), word.len()))
    .collect::<std::collections::HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
A<
B>
Cto_uppercase
Dto_lowercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' will select shorter words.
Using 'to_lowercase' will convert words to lowercase instead of uppercase.