0
0
Rustprogramming~10 mins

Arithmetic operators 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 add two numbers and print the result.

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

Complete the code to multiply two numbers and print the result.

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

Fix the error in the code to correctly divide two numbers and print the result as a float.

Rust
fn main() {
    let result = 10.0 [1] 4.0;
    println!("{}", result);
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using integer division or wrong operator causing errors.
4fill in blank
hard

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

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

Fill all three blanks to create a map of uppercase words to their lengths for words longer than 3 characters.

Rust
fn main() {
    let words = ["apple", "cat", "banana", "dog"];
    let lengths: std::collections::HashMap<_, _> = words.iter()
        .filter(|&&word| word.len() [1] 3)
        .map(|&word| (word.[2](), word.[3]()))
        .collect();
    println!("{:?}", lengths);
}
Drag options to blanks, or click blank then click option'
A>
Blen
Cto_uppercase
Dto_lowercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Mixing up length and case conversion methods.